Sequelize.js 种子期后遗症
我有一个node.js应用程序,它使用Sequelize。我目前的目标是SQLite,以方便开发人员的安装和测试,但将转移到MySQL进行生产。我使用sequelize cli来创建模型和迁移,所有这些工作都没有任何问题,我已经确认这些表是使用SQLite浏览器工具创建的。我现在遇到的问题是,当运行下面的种子文件(数据库当前为空)时,我收到以下错误 错误:Sequelize.js 种子期后遗症,sequelize.js,Sequelize.js,我有一个node.js应用程序,它使用Sequelize。我目前的目标是SQLite,以方便开发人员的安装和测试,但将转移到MySQL进行生产。我使用sequelize cli来创建模型和迁移,所有这些工作都没有任何问题,我已经确认这些表是使用SQLite浏览器工具创建的。我现在遇到的问题是,当运行下面的种子文件(数据库当前为空)时,我收到以下错误 错误: Unhandled rejection SequelizeUniqueConstraintError: Validation error a
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 = 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中手动运行等效的插入,它们可以工作,所以我不认为是DB拒绝了插入,它似乎是内部的续集
如果有任何帮助,我将不胜感激,因为我几天来一直在尝试一些选项,但运气不佳。这个错误似乎非常误导。我不确定解决方案是否正确,但将createdAt和updatedAt属性添加到对象中可以使它们成功地进行种子设定。我的假设是ORM会自动处理这些问题。工作的种子看起来是这样的(没有对任何模型或迁移进行任何更改) 我也有同样的问题。 我添加了缺少的
createdAt
和updatedAt
列,但错误仍然存在。
我失踪了
在bulkInsert
函数的末尾。只需将时间戳:true
添加到您的模型中,就可以了
module.exports = {
......
.......
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}, {
timeStamps: true // -> Add this
}
}
非常容易引起误解的错误,你的解决方案帮助我解决了它。默认情况下,ThanksIt为true。
'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()}
], {});
}, ...
module.exports = {
......
.......
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}, {
timeStamps: true // -> Add this
}
}