在MS SQL下与关联一起迁移时的问题

时间:2018-12-02 22:53:55

标签: sql-server sequelize.js sequelize-cli

我在运行时遇到问题:在MS SQL下为sequelize db:migrate。 我有一个contact模型和另外belongsTo 2个模型。因此,我将其添加到了contact.associate = function(models)函数中:

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

联系人模型的迁移如下:

 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"
    }
  },

我要与contact模型关联的模型分别命名为:jobcustomer,其表:jobscustomers < br />
使用--debug标志运行时遇到的错误:

== 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)

没有标志:

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

没有记录以前的错误。

我已经检查了customersjobs表的主键是否命名为id,并且这些表是在数据库中创建的。
Sincerly,Jan

编辑:我已经注释掉jobIdcustomerId两个对象,然后迁移,一旦撤消,我尝试仅添加一个,但是再次失败,并出现相同的错误

3 个答案:

答案 0 :(得分:1)

好的, 通过为外键创建单独的迁移,我设法解决了该问题:

  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',
    )
  })
}

答案 1 :(得分:0)

删除ms-sql中的sequelize元文件并重新运行迁移。

答案 2 :(得分:0)

我遇到了类似的错误,所以我尝试在 MSSQL 上运行 sql 语句并设法得到以前的错误:

<块引用>

在表 'tbQuestions' 上引入 FOREIGN KEY 约束 'FK__tbQuestio__userU__232A17DA' 可能会导致循环或多个级联路径

我有两个表连接到另一个表,所以我不得不添加 onDelete: "NO ACTION" 以防止错误并起作用。我不知道是否值得一提,但有人可以尝试一下。