One to Many problem

I am trying to make a form that let's you add a product, with different variants. Example Product: Journal, Variants (blue journal, red journal). So I made two schemas and enforced a one-to-many relation. Now the problem is...I can create a product without having a variation. What would be a good solution for this, where a user needs to upload at least one variant, but can also have an array of variants to be uploaded?
import { InferSelectModel, relations } from "drizzle-orm";
import {
pgTable,
serial,
text,
timestamp,
real,
integer,
} from "drizzle-orm/pg-core";

export const users = pgTable("users", {
id: serial("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull(),
image: text("image").notNull(),
clerkId: text("clerkId").notNull().unique(),
stripeCustomer: text("stripeCustomer").notNull().unique(),
createdAt: timestamp("createdAt").defaultNow().notNull(),
});

export const products = pgTable("products", {
id: serial("id").primaryKey(),
description: text("description").notNull(),
price: real("price").notNull(),
title: text("title").notNull(),
subtitle: text("subtitle").notNull(),
});

export const productVariant = pgTable("productVariant", {
id: serial("id").primaryKey(),
image: text("image").notNull(),
color: text("color").notNull(),
variantName: text("variantName").notNull(),
productID: integer("productID").references(() => products.id),
});

export const productVariantRelations = relations(productVariant, ({ one }) => ({
product: one(products, {
fields: [productVariant.productID],
references: [products.id],
}),
}));

export const productRelations = relations(products, ({ many }) => ({
productVariants: many(productVariant),
}));

export type Products = InferSelectModel<typeof users>;
export type ProductVariants = InferSelectModel<typeof productVariant>;
export type User = InferSelectModel<typeof users>;
import { InferSelectModel, relations } from "drizzle-orm";
import {
pgTable,
serial,
text,
timestamp,
real,
integer,
} from "drizzle-orm/pg-core";

export const users = pgTable("users", {
id: serial("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull(),
image: text("image").notNull(),
clerkId: text("clerkId").notNull().unique(),
stripeCustomer: text("stripeCustomer").notNull().unique(),
createdAt: timestamp("createdAt").defaultNow().notNull(),
});

export const products = pgTable("products", {
id: serial("id").primaryKey(),
description: text("description").notNull(),
price: real("price").notNull(),
title: text("title").notNull(),
subtitle: text("subtitle").notNull(),
});

export const productVariant = pgTable("productVariant", {
id: serial("id").primaryKey(),
image: text("image").notNull(),
color: text("color").notNull(),
variantName: text("variantName").notNull(),
productID: integer("productID").references(() => products.id),
});

export const productVariantRelations = relations(productVariant, ({ one }) => ({
product: one(products, {
fields: [productVariant.productID],
references: [products.id],
}),
}));

export const productRelations = relations(products, ({ many }) => ({
productVariants: many(productVariant),
}));

export type Products = InferSelectModel<typeof users>;
export type ProductVariants = InferSelectModel<typeof productVariant>;
export type User = InferSelectModel<typeof users>;
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server