nodejh / sequelize-automate

Automatically generate bare sequelize models from your database.
MIT License
116 stars 22 forks source link

SequelizeDatabaseError: Could not create constraint or index. See previous errors. #26

Open hsynlms opened 4 years ago

hsynlms commented 4 years ago

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.

  1. product_platforms table's identity column (auto-incremented) cannot have a default value (validation passes when I delete defaultValue prop in definition).
  2. product_id foreign key belongs to a never exists table. The table it references to should have been product not product_model. There is a mismatch (validation passes when I delete _model suffix).
  3. All of indexed tables have type: "BTREE" prop in their model definition files and cause SQL invalidations.