-- AlterTable
ALTER TABLE "goods" ADD COLUMN     "family_id" BIGINT;

-- CreateIndex
CREATE INDEX "goods_family_id_idx" ON "goods"("family_id");

-- AddForeignKey
ALTER TABLE "goods" ADD CONSTRAINT "goods_family_id_fkey" FOREIGN KEY ("family_id") REFERENCES "product_families"("family_id") ON DELETE SET NULL ON UPDATE NO ACTION;

-- Backfill: Good 的族 = 其主链接所属族（派生数据，后续由服务层联动维护）
UPDATE "goods" SET "family_id" = (
  SELECT "o"."family_id" FROM "origin_goods" "o"
  WHERE "o"."origin_good_id" = "goods"."origin_good_id"
);
