I have a node.js application that uses Sequelize. I am currently targeting SQLite for easy dev installation and testing, but will migrate to MySQL for production. I used sequelize-cli to create models and migrations, and everything worked without any problems. I confirmed that the tables were created using the SQLite browser tool. The problem that I have now is that I am running the seed file, which I have below (the database is empty). I get the following error.
Error:
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)
Migration:
'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'); } };
Model:
'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; };
Seed:
'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'} ], {}); } ...
I tried looking for documentation and search queries for answers, nothing seems to hint that this is a common problem. I did not define any unique columns (except, of course, the primary key, but this is auto-increment). If I had more information or hints as to which column caused the problem from cli, I would at least be able to try some different things, but there is no help. I tried to run equivalent inserts manually in SQL, and they work, so I donβt think it refuses to insert, it seems to be something internal to Sequelize.
Any help would be greatly appreciated, as I have tried several times to complete several options, but no luck.