sequelize-automate
sequelize-automate copied to clipboard
SequelizeDatabaseError: Could not create constraint or index. See previous errors.
I used the sequelize-automate library to generate models for MySQL/MariaDB database which is currently used by one of our projects. My aim was to get models from MySQL instance and create current tables for the MSSQL database as well. Briefly, I wanted to migrate definitions of tables and columns from MySQL to MSSQL.
Something went wrong during that process. Generated definitions (so SQL code) had had some invalidations.
SSMS fired an error when I just tried to execute the SQL code below generated by sequelize.sync() function.
Executing (default):
IF OBJECT_ID('[product_platforms]', 'U') IS NULL
CREATE TABLE [product_platforms] (
[id] INTEGER NOT NULL IDENTITY(1,1) DEFAULT NULL,
[product_id] INTEGER DEFAULT NULL,
PRIMARY KEY ([id]),
FOREIGN KEY ([product_id]) REFERENCES [product_model] ([id])
);
sequelize.js:1187
SequelizeDatabaseError: Could not create constraint or index. See previous errors.
Here is a list of the things that may cause similar errors above and thus people may not have a chance to import models to the desired database.
product_platformstable's identity column (auto-incremented) cannot have a default value (validation passes when I deletedefaultValueprop in definition).product_idforeign key belongs to a never exists table. The table it references to should have beenproductnotproduct_model. There is a mismatch (validation passes when I delete _model suffix).- All of indexed tables have
type: "BTREE"prop in their model definition files and cause SQL invalidations.