42

Sequelize's document doesn't say a whole lot about autoIncrement. It only includes the following example:

// autoIncrement can be used to create auto_incrementing integer columns
incrementMe: { type: Sequelize.INTEGER, autoIncrement: true }

Based off of this example I have the following code:

db.define('Entries', {
    id: {
        type: Seq.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    title: {
        type: Seq.STRING,
        allowNull: false
    },
    entry: {
        type: Seq.TEXT,
        allowNull: false
    }
}

Now when I want to create an entry I want to do something like this:

models.Entry.create({title:'first entry', entry:'yada yada yada'})

However, when I execute that code I get a database error:

Null value in column "id" violates not null constraint

I would assume Sequelize would put the integer in for me or define the database table to do that itself. Apparently not? What am I missing here to ensure that the id is automatically incremented and filled?

Thanks much.

Eric H.
  • 6,894
  • 8
  • 43
  • 62

6 Answers6

31

Normally Sequelize.js adapt itself with the database. So the autoincrement attribute corresponds to a serial type in PostgreSQL.

I already worked with Sequelize and what I remember is that you don't need to specify an id as a primary key. Sequelize will do it for you.

Try to not add an id attribute and see if Sequelize will not add it for you or not.

gvlasov
  • 18,638
  • 21
  • 74
  • 110
Yacine Rezgui
  • 1,771
  • 1
  • 18
  • 15
13

omitNull config option by default is false. Set it to true, and the id will be taken care of by Postgres:

sequelize = new Sequelize('mydb', 'postgres', null, {
  host: 'localhost',
  port: 5432,
  dialect: 'postgres',
  omitNull: true
})

Visitor = sequelize.define('visitor', {
  email: Sequelize.STRING
})

Visitor.create({email: 'foo@bar.com'})
mxgrn
  • 1,733
  • 16
  • 20
  • 8
    As per [this reported issue](https://github.com/sequelize/sequelize/issues/2813), `omitNull` should no longer be used. Use `autoIncrement: true` instead in the corresponding column at the model definition and simply don't provide a value when you do the `create()`. – Thalis K. Feb 15 '15 at 11:29
  • 3
    @ThalisK. : This does not work for me. Have the same problem as the author. – akohout Jul 14 '15 at 08:42
  • @mxgrn, thanks its works for me, if you dont ommit null value, postgresql will put null value inside statement. https://github.com/sequelize/sequelize/issues/925 – Agung Prasetyo Aug 17 '15 at 07:29
  • @akohout Have you find a solution? – Kannan T Oct 03 '17 at 17:27
6

It works for me if I add the "id" field manually (in this case, Sequelize will not add it automatically) in the model like this:

Visitor = sequelize.define('visitor', {
  id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
  },
  email: Sequelize.STRING
})

Resultant SQL command is: CREATE TABLE IF NOT EXISTS "visitor" ("id" SERIAL, "email" varchar(255)...

Dominik
  • 6,078
  • 8
  • 37
  • 61
AdelaF
  • 71
  • 1
  • 2
3

In my case the problem was that the table definition was not updated. If there is no valuable data, try removing the table and then have Sequelize recreate that table.

gvlasov
  • 18,638
  • 21
  • 74
  • 110
1

In my case this was because I had specified autoincrement: true instead of autoIncrement: true.

Worth checking if you are still having problems. Tables needed dropping and rebuilding for it to work.

meesern
  • 2,518
  • 27
  • 29
0

Make sure that when you created your table, you did so with the SERIAL data type for your id column, like so:

CREATE TABLE Entries (
    id SERIAL PRIMARY KEY,
    # other values...
);

My problem was that I was making id the INTEGER data type. In addition to that sin, I was trying to do so with the AUTO_INCREMENT constraint, which doesn't exist in Postgres (to my knowledge).

As has been stated above, you can also drop all your tables and remake them by utilizing sequelize.sync(), which will create the perfect tables for your model.

JCollier
  • 1,102
  • 2
  • 19
  • 31