sequelize / sequelize

Feature-rich ORM for modern Node.js and TypeScript, it supports PostgreSQL (with JSON and JSONB support), MySQL, MariaDB, SQLite, MS SQL Server, Snowflake, Oracle DB (v6), DB2 and DB2 for IBM i.
https://sequelize.org/
MIT License
29.25k stars 4.25k forks source link

QueryInterface removeConstraint method fails to remove CHECK constraints (MySQL) #17382

Open fullmetalsheep opened 2 weeks ago

fullmetalsheep commented 2 weeks ago

Issue Creation Checklist

Bug Description

Context: I'm using sequelize migrations to drop an existing CHECK constraint on one of our tables. The dialect is MySQL Community Server 8.1

When removeConstraint is invoked with correct schema and constraint name, it returns this error: Error Code: 1091. Can't DROP 'constraint_to_delete'; check that column/key exists. I have verified that this constraint definitely exists.

I debugged removeConstraint function further with logging and found out that internally it runs these SQL commands:

SELECT CONSTRAINT_CATALOG AS constraintCatalog, CONSTRAINT_NAME AS constraintName, CONSTRAINT_SCHEMA AS constraintSchema, CONSTRAINT_TYPE AS constraintType, 
TABLE_NAME AS tableName, TABLE_SCHEMA AS tableSchema from INFORMATION_SCHEMA.TABLE_CONSTRAINTS 

WHERE table_name='mytable' AND constraint_name = 'constraint_to_delete' AND TABLE_SCHEMA = 'mydatabase';

DROP INDEX constraint_to_delete on mytable;

Note that it tries to use DROP INDEX to drop the constraint. At least in MySQL DROP INDEX doesnt seem to work for dropping constraints.

If I run this SQL query ALTER TABLE mytable DROP CONSTRAINT constraint_to_delete It gets removed as expected.

Reproducible Example

Here is the link to the SSCCE for this issue: n/a

What do you expect to happen?

The CHECK constraint should be dropped.

What is actually happening?

removeConstraint throws this error: Error Code: 1091. Can't DROP 'constraint_to_delete'; check that column/key exists despite the CHECK constraint definitely existing.

Environment

Would you be willing to resolve this issue by submitting a Pull Request?


Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

WikiRik commented 2 weeks ago

I believe we've done some work on this in the v7 alphas. Can you check with installing @sequelize/core@alpha?