How does autoIncrement work in NodeJs's Sequelize?

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.


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: '[email protected]'})

It worked for me if I added manualy the "id" field (in this case Sequelize will not add it automaticaly) in the model like this:

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

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