From 848eed0b6fe52ba65b79e5ed5ff3e49b01bb901b Mon Sep 17 00:00:00 2001 From: yeuimu <2197651308@qq.com> Date: Thu, 27 Aug 2026 18:09:00 +0800 Subject: [PATCH] feat(db): add good_origin_goods junction table for merged origin goods --- .../migration.sql | 17 +++++++++++++++++ apps/api/prisma/migrations/migration_lock.toml | 4 ++-- apps/api/prisma/schema.prisma | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 apps/api/prisma/migrations/20260827100523_add_good_origin_goods/migration.sql diff --git a/apps/api/prisma/migrations/20260827100523_add_good_origin_goods/migration.sql b/apps/api/prisma/migrations/20260827100523_add_good_origin_goods/migration.sql new file mode 100644 index 0000000..6905354 --- /dev/null +++ b/apps/api/prisma/migrations/20260827100523_add_good_origin_goods/migration.sql @@ -0,0 +1,17 @@ +-- CreateTable +CREATE TABLE "good_origin_goods" ( + "good_id" BIGINT NOT NULL, + "origin_good_id" BIGINT NOT NULL, + "created_at" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "good_origin_goods_pkey" PRIMARY KEY ("good_id","origin_good_id") +); + +-- CreateIndex +CREATE INDEX "good_origin_goods_origin_good_id_idx" ON "good_origin_goods"("origin_good_id"); + +-- AddForeignKey +ALTER TABLE "good_origin_goods" ADD CONSTRAINT "good_origin_goods_good_id_fkey" FOREIGN KEY ("good_id") REFERENCES "goods"("good_id") ON DELETE CASCADE ON UPDATE NO ACTION; + +-- AddForeignKey +ALTER TABLE "good_origin_goods" ADD CONSTRAINT "good_origin_goods_origin_good_id_fkey" FOREIGN KEY ("origin_good_id") REFERENCES "origin_goods"("origin_good_id") ON DELETE CASCADE ON UPDATE NO ACTION; diff --git a/apps/api/prisma/migrations/migration_lock.toml b/apps/api/prisma/migrations/migration_lock.toml index 6bf9015..fbffa92 100644 --- a/apps/api/prisma/migrations/migration_lock.toml +++ b/apps/api/prisma/migrations/migration_lock.toml @@ -1,3 +1,3 @@ -# Please do not edit this file manually -# It should be added in your version-control system (i.e. Git) +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) provider = "postgresql" \ No newline at end of file diff --git a/apps/api/prisma/schema.prisma b/apps/api/prisma/schema.prisma index 71ed5bd..25b9408 100644 --- a/apps/api/prisma/schema.prisma +++ b/apps/api/prisma/schema.prisma @@ -36,6 +36,7 @@ model OriginGood { goods Good[] detail OriginGoodDetail? variants OriginGoodVariant[] + mergedIntoGoods GoodOriginGood[] @@index([sdsCategoryId]) @@index([source]) @@ -215,6 +216,7 @@ model Good { tag Tag? @relation(fields: [tagId], references: [id], onDelete: SetNull, onUpdate: NoAction) position Position? @relation(fields: [positionId], references: [id], onDelete: SetNull, onUpdate: NoAction) goodTags GoodTag[] + mergedOriginGoods GoodOriginGood[] @@index([originGoodId]) @@index([countryId]) @@ -241,6 +243,21 @@ model GoodTag { @@map("good_tags") } +// ---------- Good-OriginGood Junction (merged secondary sources, M:N) ---------- +// Primary source stays on goods.origin_good_id and is NOT stored here. +model GoodOriginGood { + goodId BigInt @map("good_id") + originGoodId BigInt @map("origin_good_id") + createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6) + + good Good @relation(fields: [goodId], references: [id], onDelete: Cascade, onUpdate: NoAction) + originGood OriginGood @relation(fields: [originGoodId], references: [id], onDelete: Cascade, onUpdate: NoAction) + + @@id([goodId, originGoodId]) + @@index([originGoodId]) + @@map("good_origin_goods") +} + // ---------- Users (admin authentication) ---------- enum Role { ADMIN