cli: ERROR: null value in column "createdAt" violates not-null constraint

What are you doing?

I’m following the tutorial here: http://docs.sequelizejs.com/manual/tutorial/migrations.html#creating-first-model-and-migration-

But when I do:

node_modules/.bin/sequelize db:seed:all

it gives to me:

ERROR: null value in column "createdAt" violates not-null constraint

I tested in a new npm init project with a new Postgres DB 10.4.

Dialect: postgres Dialect version: “pg”: “7.4.3”, Database version: 10.4 Sequelize version: “sequelize”: “4.38.0” Tested with latest release: No

UPDATE:

Maybe this is a way to go? http://mmiyauchi.com/?p=2057

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 15 (2 by maintainers)

Commits related to this issue

Most upvoted comments

@frederikhors As the error message suggests, createdAt (and also updatedAt) cannot be NULL. You need to add them with values in the bulk insert objects.

In the tutorial, it is likely that either the timestamps for the table did not have a NOT NULL constraint or they did not exist as columns on the table.

Simply add them with the current date (new Date()) as in the following:

'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.bulkInsert('Users', [{
        firstName: 'John',
        lastName: 'Doe',
        email: 'demo@demo.com',
        // add createdAt, updatedAt
        createdAt: new Date(),
        updatedAt: new Date()
      }], {});
  }
};

@frederikhors I could be wrong, but I haven’t seen anywhere demonstrating that sequelize handles it for you.

However I THINK (have not tested), you could add a new Date() as a default value to the Users schema.

In the migration:

createdAt: {
  allowNull: false,
  defaultValue: new Date(),
  type: Sequelize.DATE
},
updatedAt: {
  allowNull: false,
  defaultValue: new Date(),
  type: Sequelize.DATE
}

I found the answer; for some reason createdAt and updatedAt were added to the SequelizeMeta table – but the migration was adding a record to the SequelizeMeta table without date values and it was THAT query that was breaking the migration.

For v6+

createdAt: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.NOW, }

The timestamps in the metadata table are probably because of: sequelize db:migrate:schema:timestamps:add

I deleted all the tables in my database (including sequelizeMeta) and rebuilt them by running my migrations.

The key here is that for some reason the timestamps were added to my SequelizeMeta table but Sequelize doesn’t expect them to be there so the inserts have no timestamp (createdAt/updatedAt) data, and therefore, fail.

When they were rebuild the sequelizeMeta tables no longer have timestamps and the problem does not occur. I think in retrospect I could have just deleted the sequelizeMeta. Obviously in production you’ll want to back up your data first.

Alternately create a fresh database, run your migrations there, and import your data from your old database, then point your system to the new database.