diff --git a/prisma/migrations/20250820183901_add_tables_variants_products/migration.sql b/prisma/migrations/20250820183901_add_tables_variants_products/migration.sql new file mode 100644 index 0000000..f64a02c --- /dev/null +++ b/prisma/migrations/20250820183901_add_tables_variants_products/migration.sql @@ -0,0 +1,43 @@ +/* + Warnings: + + - You are about to drop the column `price` on the `products` table. All the data in the column will be lost. + +*/ +-- AlterTable +ALTER TABLE "products" DROP COLUMN "price"; + +-- CreateTable +CREATE TABLE "variants_attributes" ( + "id" SERIAL NOT NULL, + "name" TEXT NOT NULL, + "created_at" TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "variants_attributes_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "variants_attributes_values" ( + "id" SERIAL NOT NULL, + "attribute_id" INTEGER NOT NULL, + "product_id" INTEGER NOT NULL, + "value" TEXT NOT NULL, + "price" DECIMAL(10,2) NOT NULL, + "created_at" TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "variants_attributes_values_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "variants_attributes_name_key" ON "variants_attributes"("name"); + +-- CreateIndex +CREATE UNIQUE INDEX "variants_attributes_values_attribute_id_product_id_value_key" ON "variants_attributes_values"("attribute_id", "product_id", "value"); + +-- AddForeignKey +ALTER TABLE "variants_attributes_values" ADD CONSTRAINT "variants_attributes_values_attribute_id_fkey" FOREIGN KEY ("attribute_id") REFERENCES "variants_attributes"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "variants_attributes_values" ADD CONSTRAINT "variants_attributes_values_product_id_fkey" FOREIGN KEY ("product_id") REFERENCES "products"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index e0f992b..d251f4a 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -55,7 +55,6 @@ model Product { title String imgSrc String @map("img_src") alt String? - price Decimal @db.Decimal(10, 2) description String? categoryId Int? @map("category_id") isOnSale Boolean @default(false) @map("is_on_sale") @@ -63,13 +62,41 @@ model Product { createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(0) updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(0) - category Category? @relation(fields: [categoryId], references: [id], onDelete: SetNull) - cartItems CartItem[] - orderItems OrderItem[] + category Category? @relation(fields: [categoryId], references: [id], onDelete: SetNull) + cartItems CartItem[] + orderItems OrderItem[] + variantAttributeValues VariantAttributeValue[] @@map("products") } +model VariantAttribute { + id Int @id @default(autoincrement()) + name String @unique + createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(0) + updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(0) + + variantsAttributeValue VariantAttributeValue[] + + @@map("variants_attributes") +} + +model VariantAttributeValue { + id Int @id @default(autoincrement()) + attributeId Int @map("attribute_id") + productId Int @map("product_id") + value String + price Decimal @db.Decimal(10, 2) + createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(0) + updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(0) + + variantAttribute VariantAttribute @relation(fields: [attributeId], references: [id]) + product Product @relation(fields: [productId], references: [id]) + + @@unique([attributeId, productId, value], name: "unique_attribute_product_value") + @@map("variants_attributes_values") +} + model Cart { id Int @id @default(autoincrement()) sessionCartId String @unique @default(dbgenerated("gen_random_uuid()")) @map("session_cart_id") @db.Uuid