Skip to content

Commit

Permalink
fix incorrect fk constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
allgood committed Jan 16, 2025
1 parent 4d2bd33 commit c8b96ae
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default {
`
DELETE FROM "WhatsappQueues"
WHERE "whatsappId" NOT IN (SELECT "id" FROM "Whatsapps")
OR "queueId" NOT IN (SELECT "id" FROM "Users")
`,
{ transaction }
);
Expand All @@ -26,32 +25,13 @@ export default {
onDelete: "CASCADE",
transaction
});

await queryInterface.addConstraint("WhatsappQueues", {
fields: ["queueId"],
type: "foreign key",
name: "fk_whatsappqueues_queueid",
references: {
table: "Users",
field: "id"
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
transaction
});
});
},

down: (queryInterface: QueryInterface) => {
return Promise.all([
queryInterface.removeConstraint(
"WhatsappQueues",
"fk_whatsappqueues_whatsappid"
),
queryInterface.removeConstraint(
"WhatsappQueues",
"fk_whatsappqueues_queueid"
)
]);
return queryInterface.removeConstraint(
"WhatsappQueues",
"fk_whatsappqueues_whatsappid"
);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { QueryInterface } from "sequelize";

export default {
up: async (queryInterface: QueryInterface) => {
await queryInterface.sequelize.transaction(async transaction => {
// Remove invalid constraints
try {
await queryInterface.removeConstraint(
"WhatsappQueues",
"fk_whatsappqueues_queueid"
);
} catch (error) {
// ignore error
}

// Remove invalid references
await queryInterface.sequelize.query(
`
DELETE FROM "WhatsappQueues"
WHERE "queueId" NOT IN (SELECT "id" FROM "Queues")
`,
{ transaction }
);

await queryInterface.addConstraint("WhatsappQueues", {
fields: ["queueId"],
type: "foreign key",
name: "fk_whatsappqueues_queueid",
references: {
table: "Queues",
field: "id"
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
transaction
});
});
},

down: (queryInterface: QueryInterface) => {
return queryInterface.removeConstraint(
"WhatsappQueues",
"fk_whatsappqueues_queueid"
);
}
};

0 comments on commit c8b96ae

Please sign in to comment.