From fc4e30f1483cb807a2d4ae386a930c39d2e128fc Mon Sep 17 00:00:00 2001 From: Robson Tenorio Henriques Date: Wed, 16 Oct 2024 13:17:44 -0300 Subject: [PATCH] =?UTF-8?q?Corre=C3=A7=C3=A3o=20em=20foreign=20key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...934-alter-userId-foreign-key-on-tickets.ts | 16 ++++---- ...22-alter-queueId-foreign-key-on-tickets.ts | 40 ++++++++++--------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/backend/src/database/migrations/20240921112934-alter-userId-foreign-key-on-tickets.ts b/backend/src/database/migrations/20240921112934-alter-userId-foreign-key-on-tickets.ts index 32fcdfa2..3f05a243 100644 --- a/backend/src/database/migrations/20240921112934-alter-userId-foreign-key-on-tickets.ts +++ b/backend/src/database/migrations/20240921112934-alter-userId-foreign-key-on-tickets.ts @@ -2,13 +2,13 @@ import { QueryInterface } from "sequelize"; module.exports = { up: async (queryInterface: QueryInterface) => { - await queryInterface.removeConstraint("tickets", "tickets_ibfk_2"); + await queryInterface.removeConstraint("Tickets", "Tickets_ibfk_2"); - await queryInterface.addConstraint("tickets", ["userId"], { + await queryInterface.addConstraint("Tickets", ["userId"], { type: "foreign key", - name: "tickets_ibfk_2", + name: "Tickets_ibfk_2", references: { - table: "users", + table: "Users", field: "id" }, onDelete: "CASCADE", @@ -17,13 +17,13 @@ module.exports = { }, down: async (queryInterface: QueryInterface) => { - await queryInterface.removeConstraint("tickets", "tickets_ibfk_2"); + await queryInterface.removeConstraint("Tickets", "Tickets_ibfk_2"); - await queryInterface.addConstraint("tickets", ["userId"], { + await queryInterface.addConstraint("Tickets", ["userId"], { type: "foreign key", - name: "tickets_ibfk_2", + name: "Tickets_ibfk_2", references: { - table: "users", + table: "Users", field: "id" }, onDelete: "SET NULL", diff --git a/backend/src/database/migrations/20240921153422-alter-queueId-foreign-key-on-tickets.ts b/backend/src/database/migrations/20240921153422-alter-queueId-foreign-key-on-tickets.ts index a9c03112..045ac75c 100644 --- a/backend/src/database/migrations/20240921153422-alter-queueId-foreign-key-on-tickets.ts +++ b/backend/src/database/migrations/20240921153422-alter-queueId-foreign-key-on-tickets.ts @@ -2,41 +2,45 @@ import { QueryInterface } from "sequelize"; export default { up: async (queryInterface: QueryInterface) => { - // Remover a constraint antiga - await queryInterface.removeConstraint( - "tickets", - "Tickets_queueId_foreign_idx" - ); + const [results]: any = await queryInterface.sequelize.query(` + SELECT CONSTRAINT_NAME + FROM information_schema.KEY_COLUMN_USAGE + WHERE TABLE_NAME = 'Tickets' AND COLUMN_NAME = 'queueId'; + `); + + if (results.length > 0) { + const constraintName = results[0].CONSTRAINT_NAME; + if (constraintName) { + await queryInterface.removeConstraint("Tickets", constraintName); + } + } - // Adicionar a nova constraint com SET NULL e CASCADE - await queryInterface.addConstraint("tickets", ["queueId"], { + await queryInterface.addConstraint("Tickets", ["queueId"], { type: "foreign key", - name: "Tickets_queueId_foreign_idx", + name: "Tickets_queueId_custom_foreign", references: { - table: "queues", + table: "Queues", field: "id" }, - onDelete: "SET NULL", // Ao deletar, coloca NULL no campo queueId - onUpdate: "CASCADE" // Ao atualizar o id na tabela queues, atualiza o queueId em tickets + onDelete: "SET NULL", + onUpdate: "CASCADE" }); }, down: async (queryInterface: QueryInterface) => { - // Remover a constraint recém adicionada await queryInterface.removeConstraint( - "tickets", - "Tickets_queueId_foreign_idx" + "Tickets", + "Tickets_queueId_custom_foreign" ); - // Restaurar a constraint antiga - await queryInterface.addConstraint("tickets", ["queueId"], { + await queryInterface.addConstraint("Tickets", ["queueId"], { type: "foreign key", name: "Tickets_queueId_foreign_idx", references: { - table: "queues", + table: "Queues", field: "id" }, - onDelete: "RESTRICT", + onDelete: "SET NULL", onUpdate: "CASCADE" }); }