question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`queryInterface.removeColumn` attempts to remove non-existing constraints

See original GitHub issue

This case worries me because in some cases, Sequelize takes information from another database than from the one it is configured to use (located on the same server).

What you are doing?

I’m implementing this simple migration.

module.exports = {
	up: function (queryInterface) {
		queryInterface.removeColumn('myModel', 'colName');
	}
}

This column has no constraint (no index, no foreign key constraint), thus should be deleted with a simple ALTER TABLE … DROP … query to the DB.

However, on my MariaDB server, another database exists with the same table, but with constraints set on this column.

What do you expect to happen?

The column should be properly removed from the table.

What is actually happening?

An error is thrown:

Unhandled rejection SequelizeDatabaseError: ER_CANT_DROP_FIELD_OR_KEY: Can't DROP 'foreign_key_constraint_name'; check that column/key exists

This is because Sequelize queries the INFORMATION_SCHEMA of the whole MariaDB server to find constraints on the column before removing them if they exist.

The query to INFORMATION_SCHEMA generated by Sequelize should have a condition on the DB name (namely on the CONSTRAINT_SCHEMA column) !

Right now, it finds constraints on other databases, and tries to removes them from the current one !

Dialect: mariadb Sequelize version: 3.24.1

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:16
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
euqencommented, Jun 19, 2017

+1 faced with the same problem

1reaction
jy95commented, Dec 28, 2019

@papb It seems this bug is still here when using a schema in Postgres

let opts = {tableName: 'Exercises'};
if (queryInterface.sequelize.options.schema) {
    opts.schema = queryInterface.sequelize.options.schema;
}
return Promise.all([
    queryInterface.removeColumn(opts, "state")
]);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove constraints in sequelize migration - Stack Overflow
As of 2017 with Sequelize 4.4.2, we can remove constraints with queryInterface API: queryInterface.removeConstraint(tableName, constraintName).
Read more >
Fullstack part13 | Migrations, many-to-many relationships
Let's remove the lines that do the synchronization and move to using a much more robust way, ... removeColumn('users', 'admin') await queryInterface.
Read more >
sequelize execute a query which would set an environment or ...
When using Sequelize along with an sqlite db to remove a column via queryInterface.removeColumn the foreign key constraints from the table are removed....
Read more >
Active Record Migrations - Ruby on Rails Guides
A schema starts off with nothing in it, and each migration modifies it to add or remove tables, columns, or entries. Active Record...
Read more >
QueryInterface - Sequelize
Available constraints: UNIQUE; DEFAULT (MSSQL only); CHECK (MySQL - Ignored by the database engine ); FOREIGN KEY; PRIMARY KEY ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found