SequelizeUniqueConstraintError during sowing

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.

+5
source share
2 answers

The error seems to be extremely misleading. The solution, true or not, I'm not sure, but adding the createdAt and updatedAt attributes to the objects that allowed them to successfully seed. My guess was that ORM would automatically take care of that. The processed seed looks like this (no changes in models and transitions).

 '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()} ], {}); }, ... 
+12
source

There was the same problem. I added the missing createdAt and updatedAt , but the error still persists. I have lost ; at the end of the bulkInsert function.

0
source

Source: https://habr.com/ru/post/1236089/


All Articles