diff --git a/prisma/migrations/20251107163000_restore_constructeur_join_tables_orientation/migration.sql b/prisma/migrations/20251107163000_restore_constructeur_join_tables_orientation/migration.sql new file mode 100644 index 0000000..91ee1f2 --- /dev/null +++ b/prisma/migrations/20251107163000_restore_constructeur_join_tables_orientation/migration.sql @@ -0,0 +1,72 @@ +-- Restore the implicit many-to-many join table orientation between constructeurs +-- and machines/composants/pièces so Prisma nested writes keep using the expected +-- foreign key columns (A → constructeur, B → parent record). + +-- Machines ↔ Constructeurs +CREATE TABLE "_MachineConstructeurs_new" ( + "A" TEXT NOT NULL, + "B" TEXT NOT NULL, + CONSTRAINT "_MachineConstructeurs_new_A_fkey" FOREIGN KEY ("A") REFERENCES "constructeurs"("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "_MachineConstructeurs_new_B_fkey" FOREIGN KEY ("B") REFERENCES "machines"("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +INSERT INTO "_MachineConstructeurs_new" ("A", "B") +SELECT mc."B", mc."A" +FROM "_MachineConstructeurs" mc +JOIN "machines" m ON mc."B" = m."id" +JOIN "constructeurs" c ON mc."A" = c."id"; + +DROP TABLE "_MachineConstructeurs"; + +ALTER TABLE "_MachineConstructeurs_new" RENAME TO "_MachineConstructeurs"; +ALTER TABLE "_MachineConstructeurs" RENAME CONSTRAINT "_MachineConstructeurs_new_A_fkey" TO "_MachineConstructeurs_A_fkey"; +ALTER TABLE "_MachineConstructeurs" RENAME CONSTRAINT "_MachineConstructeurs_new_B_fkey" TO "_MachineConstructeurs_B_fkey"; + +CREATE UNIQUE INDEX "_MachineConstructeurs_AB_unique" ON "_MachineConstructeurs"("A", "B"); +CREATE INDEX "_MachineConstructeurs_B_index" ON "_MachineConstructeurs"("B"); + +-- Composants ↔ Constructeurs +CREATE TABLE "_ComposantConstructeurs_new" ( + "A" TEXT NOT NULL, + "B" TEXT NOT NULL, + CONSTRAINT "_ComposantConstructeurs_new_A_fkey" FOREIGN KEY ("A") REFERENCES "constructeurs"("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "_ComposantConstructeurs_new_B_fkey" FOREIGN KEY ("B") REFERENCES "composants"("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +INSERT INTO "_ComposantConstructeurs_new" ("A", "B") +SELECT cc."B", cc."A" +FROM "_ComposantConstructeurs" cc +JOIN "composants" comp ON cc."B" = comp."id" +JOIN "constructeurs" c ON cc."A" = c."id"; + +DROP TABLE "_ComposantConstructeurs"; + +ALTER TABLE "_ComposantConstructeurs_new" RENAME TO "_ComposantConstructeurs"; +ALTER TABLE "_ComposantConstructeurs" RENAME CONSTRAINT "_ComposantConstructeurs_new_A_fkey" TO "_ComposantConstructeurs_A_fkey"; +ALTER TABLE "_ComposantConstructeurs" RENAME CONSTRAINT "_ComposantConstructeurs_new_B_fkey" TO "_ComposantConstructeurs_B_fkey"; + +CREATE UNIQUE INDEX "_ComposantConstructeurs_AB_unique" ON "_ComposantConstructeurs"("A", "B"); +CREATE INDEX "_ComposantConstructeurs_B_index" ON "_ComposantConstructeurs"("B"); + +-- Pièces ↔ Constructeurs +CREATE TABLE "_PieceConstructeurs_new" ( + "A" TEXT NOT NULL, + "B" TEXT NOT NULL, + CONSTRAINT "_PieceConstructeurs_new_A_fkey" FOREIGN KEY ("A") REFERENCES "constructeurs"("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "_PieceConstructeurs_new_B_fkey" FOREIGN KEY ("B") REFERENCES "pieces"("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +INSERT INTO "_PieceConstructeurs_new" ("A", "B") +SELECT pc."B", pc."A" +FROM "_PieceConstructeurs" pc +JOIN "pieces" p ON pc."B" = p."id" +JOIN "constructeurs" c ON pc."A" = c."id"; + +DROP TABLE "_PieceConstructeurs"; + +ALTER TABLE "_PieceConstructeurs_new" RENAME TO "_PieceConstructeurs"; +ALTER TABLE "_PieceConstructeurs" RENAME CONSTRAINT "_PieceConstructeurs_new_A_fkey" TO "_PieceConstructeurs_A_fkey"; +ALTER TABLE "_PieceConstructeurs" RENAME CONSTRAINT "_PieceConstructeurs_new_B_fkey" TO "_PieceConstructeurs_B_fkey"; + +CREATE UNIQUE INDEX "_PieceConstructeurs_AB_unique" ON "_PieceConstructeurs"("A", "B"); +CREATE INDEX "_PieceConstructeurs_B_index" ON "_PieceConstructeurs"("B"); diff --git a/prisma/migrations/20251107171000_normalize_constructeur_join_tables/migration.sql b/prisma/migrations/20251107171000_normalize_constructeur_join_tables/migration.sql new file mode 100644 index 0000000..44046c8 --- /dev/null +++ b/prisma/migrations/20251107171000_normalize_constructeur_join_tables/migration.sql @@ -0,0 +1,73 @@ +-- Ensure implicit many-to-many join tables between machines/composants/pièces +-- and constructeurs follow Prisma's expected orientation: +-- A -> parent model (machines/composants/pièces) +-- B -> constructeurs + +-- Machines ↔ Constructeurs +CREATE TABLE "_MachineConstructeurs_new" ( + "A" TEXT NOT NULL, + "B" TEXT NOT NULL, + CONSTRAINT "_MachineConstructeurs_new_A_fkey" FOREIGN KEY ("A") REFERENCES "machines"("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "_MachineConstructeurs_new_B_fkey" FOREIGN KEY ("B") REFERENCES "constructeurs"("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +INSERT INTO "_MachineConstructeurs_new" ("A", "B") +SELECT mc."B", mc."A" +FROM "_MachineConstructeurs" mc +JOIN "machines" m ON mc."B" = m."id" +JOIN "constructeurs" c ON mc."A" = c."id"; + +DROP TABLE "_MachineConstructeurs"; + +ALTER TABLE "_MachineConstructeurs_new" RENAME TO "_MachineConstructeurs"; +ALTER TABLE "_MachineConstructeurs" RENAME CONSTRAINT "_MachineConstructeurs_new_A_fkey" TO "_MachineConstructeurs_A_fkey"; +ALTER TABLE "_MachineConstructeurs" RENAME CONSTRAINT "_MachineConstructeurs_new_B_fkey" TO "_MachineConstructeurs_B_fkey"; + +CREATE UNIQUE INDEX "_MachineConstructeurs_AB_unique" ON "_MachineConstructeurs"("A", "B"); +CREATE INDEX "_MachineConstructeurs_B_index" ON "_MachineConstructeurs"("B"); + +-- Composants ↔ Constructeurs +CREATE TABLE "_ComposantConstructeurs_new" ( + "A" TEXT NOT NULL, + "B" TEXT NOT NULL, + CONSTRAINT "_ComposantConstructeurs_new_A_fkey" FOREIGN KEY ("A") REFERENCES "composants"("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "_ComposantConstructeurs_new_B_fkey" FOREIGN KEY ("B") REFERENCES "constructeurs"("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +INSERT INTO "_ComposantConstructeurs_new" ("A", "B") +SELECT cc."B", cc."A" +FROM "_ComposantConstructeurs" cc +JOIN "composants" comp ON cc."B" = comp."id" +JOIN "constructeurs" c ON cc."A" = c."id"; + +DROP TABLE "_ComposantConstructeurs"; + +ALTER TABLE "_ComposantConstructeurs_new" RENAME TO "_ComposantConstructeurs"; +ALTER TABLE "_ComposantConstructeurs" RENAME CONSTRAINT "_ComposantConstructeurs_new_A_fkey" TO "_ComposantConstructeurs_A_fkey"; +ALTER TABLE "_ComposantConstructeurs" RENAME CONSTRAINT "_ComposantConstructeurs_new_B_fkey" TO "_ComposantConstructeurs_B_fkey"; + +CREATE UNIQUE INDEX "_ComposantConstructeurs_AB_unique" ON "_ComposantConstructeurs"("A", "B"); +CREATE INDEX "_ComposantConstructeurs_B_index" ON "_ComposantConstructeurs"("B"); + +-- Pièces ↔ Constructeurs +CREATE TABLE "_PieceConstructeurs_new" ( + "A" TEXT NOT NULL, + "B" TEXT NOT NULL, + CONSTRAINT "_PieceConstructeurs_new_A_fkey" FOREIGN KEY ("A") REFERENCES "pieces"("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "_PieceConstructeurs_new_B_fkey" FOREIGN KEY ("B") REFERENCES "constructeurs"("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +INSERT INTO "_PieceConstructeurs_new" ("A", "B") +SELECT pc."B", pc."A" +FROM "_PieceConstructeurs" pc +JOIN "pieces" p ON pc."B" = p."id" +JOIN "constructeurs" c ON pc."A" = c."id"; + +DROP TABLE "_PieceConstructeurs"; + +ALTER TABLE "_PieceConstructeurs_new" RENAME TO "_PieceConstructeurs"; +ALTER TABLE "_PieceConstructeurs" RENAME CONSTRAINT "_PieceConstructeurs_new_A_fkey" TO "_PieceConstructeurs_A_fkey"; +ALTER TABLE "_PieceConstructeurs" RENAME CONSTRAINT "_PieceConstructeurs_new_B_fkey" TO "_PieceConstructeurs_B_fkey"; + +CREATE UNIQUE INDEX "_PieceConstructeurs_AB_unique" ON "_PieceConstructeurs"("A", "B"); +CREATE INDEX "_PieceConstructeurs_B_index" ON "_PieceConstructeurs"("B");