Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SequelizeUniqueConstraintError during seed

Tags:

sequelize.js

I have a node.js application that uses Sequelize. I'm currently targeting SQLite for easy dev setup and testing but will be moving to MySQL for production. I used the sequelize-cli to create the models and migrations and all worked without any issues, I have confirmed that the tables were created using a SQLite browser tool. The problem I have now is when running the seed file that I have below (The database is currently empty) I receive 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)

The 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');
  }
};

The 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;
};

The 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 have tried looking though the documentation and googling for answers, nothing seems to hint at this being a common problem. I didn't define any unique columns (other than the primary key of course, but it's an autoIncrement) If I had more information or clues as to which column was causing the issue from the cli then I would at least be able to try some different things, but no help there. I've tried running the equivalent inserts manually in SQL and they work, so I don't think it's the DB rejecting the insert, it appears to be something internal to Sequelize.

Any help would be greatly appreciated as I have been trying a few options for a few days now with no luck.

like image 289
SnareChops Avatar asked Nov 16 '15 22:11

SnareChops


People also ask

How do I validate a phone number in Sequelize?

phone: { type: Sequelize. STRING, allowNull: false, validate: { notNull: { args: true, msg: "You must enter Phone Number" }, len: { args: [11,11], msg: 'Phone Number is invalid' }, isInt: { args: true, msg: "You must enter Phone Number" }, } }, node. js.

How do you make a field required in Sequelize?

If you're using Sequelize, the equivalent of required: true on mongoose is just allowNull: false . You'd write something along these lines: const YourTable = sequelize. define('your_table', { firstname: { type: Sequelize.

What is Sequelize constraint?

Constraints are the rules that we define on the level of SQL. For example, if one attribute is supposed to be unique then this will be handled on the level of SQL. If a constraint check fails then Sequelize will forward the error message sent by the database. Here an SQL query will be performed no matter what.


1 Answers

The error appears to be extremely misleading. The solution, whether the correct one or not I'm not sure, but adding the createdAt and updatedAt attributes to the objects allowed them to seed successfully. My assumption was that these would be taken care of automatically by the ORM. The seed that worked looks like this (no changes to any models or migrations).

'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()}
    ], {});
  }, ...
like image 104
SnareChops Avatar answered Jan 04 '23 04:01

SnareChops