From f3076ed41893674fc179d22251b8d8f07bae38a5 Mon Sep 17 00:00:00 2001 From: "codegen-sh[bot]" <131295404+codegen-sh[bot]@users.noreply.github.com> Date: Wed, 17 Sep 2025 08:57:16 -0400 Subject: [PATCH] Fix `shares_collectionId_fkey` constraint error with CASCADE delete (#10196) * Fix foreign key constraint error for shares.collectionId - Add migration to update shares_collectionId_fkey constraint with CASCADE delete - This fixes the constraint violation when deleting collections that have associated shares - Follows the same pattern as the existing shares_documentId_fkey cascade fix Fixes #10185 * Simplify migration by removing unnecessary loop and constraint array - Remove loop and constraintNames array since there's only one constraint - Directly reference 'shares_collectionId_fkey' constraint name - Keep the same functionality but with cleaner, simpler code --------- Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com> --- ...917124000-fix-shares-collection-cascade.js | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 server/migrations/20250917124000-fix-shares-collection-cascade.js diff --git a/server/migrations/20250917124000-fix-shares-collection-cascade.js b/server/migrations/20250917124000-fix-shares-collection-cascade.js new file mode 100644 index 0000000000..5d71798d7e --- /dev/null +++ b/server/migrations/20250917124000-fix-shares-collection-cascade.js @@ -0,0 +1,36 @@ +"use strict"; + +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface, Sequelize) { + // Drop the existing foreign key constraint + await queryInterface.sequelize.query( + `ALTER TABLE "shares" DROP CONSTRAINT "shares_collectionId_fkey"` + ); + + // Add the foreign key constraint with CASCADE delete + await queryInterface.sequelize.query(` + ALTER TABLE "shares" + ADD CONSTRAINT "shares_collectionId_fkey" + FOREIGN KEY("collectionId") + REFERENCES "collections" ("id") + ON DELETE CASCADE + `); + }, + + async down(queryInterface, Sequelize) { + // Drop the cascade constraint + await queryInterface.sequelize.query( + `ALTER TABLE "shares" DROP CONSTRAINT "shares_collectionId_fkey"` + ); + + // Add back the original constraint without cascade + await queryInterface.sequelize.query(` + ALTER TABLE "shares" + ADD CONSTRAINT "shares_collectionId_fkey" + FOREIGN KEY("collectionId") + REFERENCES "collections" ("id") + ON DELETE NO ACTION + `); + }, +};