种子期间的 SequelizeUniqueConstraintError

SequelizeUniqueConstraintError during seed

我有一个使用 Sequelize 的 node.js 应用程序。我目前的目标是 SQLite 以方便开发设置和测试,但将转向 MySQL 进行生产。我使用 sequelize-cli 创建模型和迁移,并且一切正常,我已经确认这些表是使用 SQLite 浏览器工具创建的。我现在遇到的问题是,当 运行 下面的种子文件(数据库当前为空)时,我收到以下错误。

错误:

Unhandled rejection SequelizeUniqueConstraintError: Validation error
at Query.formatError (/Users/abc/repos/myProject/node_modules/sequelize/lib/dialects/sqlite/query.js:231:14)
at Statement.<anonymous> (/Users/abc/repos/myProject/node_modules/sequelize/lib/dialects/sqlite/query.js:47:29)
at Statement.replacement (/Users/abc/repos/myProject/node_modules/sqlite3/lib/trace.js:20:31)

迁移:

'use strict';
module.exports = {
  up: function(queryInterface, Sequelize) {
    return queryInterface.createTable('Questions', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      type: {
        allowNull: false,
        type: Sequelize.INTEGER
      },
      text: {
        allowNull: false,
        type: Sequelize.STRING
      },
      nextQuestionId: {
        type: Sequelize.INTEGER
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: function(queryInterface, Sequelize) {
    return queryInterface.dropTable('Questions');
  }
};

型号:

'use strict';
module.exports = function(sequelize, DataTypes) {
  var Question = sequelize.define('Question', {
    id: {
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      type: DataTypes.INTEGER
    },
    type: {
      allowNull: false,
      type: DataTypes.INTEGER
    },
    text: {
      allowNull: false,
      type: DataTypes.STRING
    },
    nextQuestionId: {
      type: DataTypes.INTEGER
    }
  }, {
    classMethods: {
      associate: function(models) {
        Question.belongsTo(Question, {as: 'nextQuestion', foreignKey: 'nextQuestionId'});
        Question.hasMany(models.Answer);
        Question.hasMany(models.questionoption, {as: 'options'});
      }
    }
  });
  return Question;
};

种子:

'use strict';
module.exports = {
  up: function (queryInterface, Sequelize) {
    return queryInterface.bulkInsert('Questions', [
      {type: 0, text: 'Question A'},
      {type: 5, text: 'Question B', nextQuestionId: 4},
      {type: 5, text: 'Question C', nextQuestionId: 4},
      {type: 0, text: 'Question D'},
      {type: 0, text: 'Question E'},
      {type: 0, text: 'Question F'},
      {type: 0, text: 'Question G'},
      {type: 0, text: 'Question H'},
      {type: 0, text: 'Question I'}
    ], {});
  } ...

我已经尝试查看文档并在谷歌上搜索答案,但似乎没有任何迹象表明这是一个常见问题。我没有定义任何唯一的列(当然除了主键,但它是一个自动增量)如果我有更多关于哪个列导致 cli 问题的信息或线索,那么我至少可以尝试一些不同的事情,但没有帮助。我在 SQL 中尝试了 运行 手动等效插入并且它们有效,所以我不认为这是数据库拒绝插入,它似乎是 Sequelize 内部的东西。

任何帮助将不胜感激,因为我已经尝试了几天的几个选项,但没有成功。

该错误似乎极具误导性。解决方案是否正确我不确定,但将 createdAt 和 updatedAt 属性添加到对象允许它们成功播种。我的假设是这些将由 ORM 自动处理。有效的种子看起来像这样(没有对任何模型或迁移进行更改)。

'use strict';

module.exports = {
  up: function (queryInterface, Sequelize) {
    return queryInterface.bulkInsert('Questions', [
      {type: 0, text: 'Question A', createdAt: new Date(), updatedAt: new Date()},
      {type: 5, text: 'Question B', nextQuestionId: 4, createdAt: new Date(), updatedAt: new Date()},
      {type: 5, text: 'Question C', nextQuestionId: 4, createdAt: new Date(), updatedAt: new Date()},
      {type: 0, text: 'Question D', createdAt: new Date(), updatedAt: new Date()},
      {type: 0, text: 'Question E', createdAt: new Date(), updatedAt: new Date()},
      {type: 0, text: 'Question F', createdAt: new Date(), updatedAt: new Date()},
      {type: 0, text: 'Question G', createdAt: new Date(), updatedAt: new Date()},
      {type: 0, text: 'Question H', createdAt: new Date(), updatedAt: new Date()},
      {type: 0, text: 'Question I', createdAt: new Date(), updatedAt: new Date()}
    ], {});
  }, ...

有同样的问题。 我添加了缺失的 createdAtupdatedAt 列,但错误仍然存​​在。 bulkInsert 函数末尾缺少 ;

只需将 timeStamps:true 添加到您的模型中就可以了

module.exports = {
   ......
   .......
   updatedAt: {
    allowNull: false,
    type: Sequelize.DATE
  }
}, {
  timeStamps: true  // -> Add this
 }
}