2

I have issues, when running: sequelize db:migrate under MS SQL. I have a contact model that belongsTo 2 other models. So I added this to the contact.associate = function(models) function:

contact.belongsTo(models.job)
contact.belongsTo(models.customer)

The migration for the contact model looks like this:

 id: {
    allowNull: false,
    autoIncrement: true,
    primaryKey: true,
    type: Sequelize.INTEGER
  },
...
jobId: {
    type: Sequelize.INTEGER,
    references: {
      model: "jobs",
      key: "id"
    }
  },
customerId: {
    type: Sequelize.INTEGER,
    references: {
      model: "customers",
      key: "id"
    }
  },

The models, that I am trying to associate to the contact model are named: job and customer, their tables: jobs and customers

The error I got when running with the --debug Flag:

== 20181201143147-create-productionstep: migrating =======
ERROR: SequelizeDatabaseError: Could not create constraint or index. See         
previous errors.
at Query.formatError (C:\Users\Jan\Documents\navigatemNode\node_modules\sequelize\lib\dialects\mssql\query.js:315:12)
at Request.connection.lib.Request [as userCallback] (C:\Users\Jan\Documents\navigatemNode\node_modules\sequelize\lib\dialects\mssql\query.js:107:25)
at Request._this.callback (C:\Users\Jan\Documents\navigatemNode\node_modules\tedious\lib\request.js:60:27)
at Connection.endOfMessageMarkerReceived (C:\Users\Jan\Documents\navigatemNode\node_modules\tedious\lib\connection.js:1922:20)
at Connection.dispatchEvent (C:\Users\Jan\Documents\navigatemNode\node_modules\tedious\lib\connection.js:1004:38)
at Parser.<anonymous> (C:\Users\Jan\Documents\navigatemNode\node_modules\tedious\lib\connection.js:805:18)
at emitOne (events.js:116:13)
at Parser.emit (events.js:211:7)
at Parser.<anonymous> (C:\Users\Jan\Documents\navigatemNode\node_modules\tedious\lib\token\token-stream-parser.js:54:15)
at emitOne (events.js:116:13)
at Parser.emit (events.js:211:7)
at addChunk (C:\Users\Jan\Documents\navigatemNode\node_modules\readable-stream\lib\_stream_readable.js:291:12)
at readableAddChunk (C:\Users\Jan\Documents\navigatemNode\node_modules\readable-stream\lib\_stream_readable.js:278:11)
at Parser.Readable.push (C:\Users\Jan\Documents\navigatemNode\node_modules\readable-stream\lib\_stream_readable.js:245:10)
at Parser.Transform.push (C:\Users\Jan\Documents\navigatemNode\node_modules\readable-stream\lib\_stream_transform.js:148:32)
at Parser.afterTransform (C:\Users\Jan\Documents\navigatemNode\node_modules\readable-stream\lib\_stream_transform.js:91:10)
at Parser._transform (C:\Users\Jan\Documents\navigatemNode\node_modules\tedious\lib\token\stream-parser.js:69:9)
at Parser.Transform._read (C:\Users\Jan\Documents\navigatemNode\node_modules\readable-stream\lib\_stream_transform.js:184:10)
at Parser.Transform._write (C:\Users\Jan\Documents\navigatemNode\node_modules\readable-stream\lib\_stream_transform.js:172:83)
at doWrite (C:\Users\Jan\Documents\navigatemNode\node_modules\readable-stream\lib\_stream_writable.js:428:64)
at writeOrBuffer (C:\Users\Jan\Documents\navigatemNode\node_modules\readable-stream\lib\_stream_writable.js:417:5)
at Parser.Writable.write (C:\Users\Jan\Documents\navigatemNode\node_modules\readable-stream\lib\_stream_writable.js:334:11)
at Parser.addEndOfMessageMarker (C:\Users\Jan\Documents\navigatemNode\node_modules\tedious\lib\token\token-stream-parser.js:80:26)
at Connection.message (C:\Users\Jan\Documents\navigatemNode\node_modules\tedious\lib\connection.js:1913:32)
at Connection.dispatchEvent (C:\Users\Jan\Documents\navigatemNode\node_modules\tedious\lib\connection.js:1004:38)
at MessageIO.<anonymous> (C:\Users\Jan\Documents\navigatemNode\node_modules\tedious\lib\connection.js:884:18)
at emitNone (events.js:106:13)
at MessageIO.emit (events.js:208:7)

Without the flag:

ERROR: Could not create constraint or index. See previous errors.

There are no previous errors logged.

I have checked, that the PrimaryKey of the customers and jobs tables are named id and that the tables are created in the database
Sincerly, Jan

Edit: I have commented out both of the jobId and customerId objects and it migrates then, once undone, I have tried adding only one, but it fails with the same error again

Jan Krüger
  • 83
  • 1
  • 10

3 Answers3

1

Okay, I have managed to resolve the Issue, by creating a seperate migration for the foreign keys:

  up: (queryInterface, Sequelize) => {
return queryInterface.addColumn(
  'contacts',
  'jobId',
  {
    type: Sequelize.INTEGER,
    references: {
      model: "jobs",
      key: "id"
    },
    onDelete: 'SET NULL'
  },
)
  .then(() => {
    return queryInterface.addColumn(
      'contacts',
      'customerId',
      {
        type: Sequelize.INTEGER,
        references: {
          model: "customers",
          key: "id"
        },
        onDelete: 'SET NULL'
      }
    )
  })
},

down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn(
  'contacts',
  'jobId',
)
  .then(() => {
    return queryInterface.removeColumn(
      'contacts',
      'customerId',
    )
  })
}
Jan Krüger
  • 83
  • 1
  • 10
0

delete the sequelize meta file inside ms-sql and re run migration.

0

I had a similar error, so I tried to ran the sql statement on MSSQL and manage to get the previous error:

Introducing FOREIGN KEY constraint 'FK__tbQuestio__userU__232A17DA' on table 'tbQuestions' may cause cycles or multiple cascade paths

I had two tables connected to another one, so I had to add onDelete: "NO ACTION" to prevent the error and worked. I don't know if is worth mention it, but some one could give it a try.