Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize throwing 'id must be unique' on create

new to Sequelize library. From my understanding, 'id' is created automatically by sequelize (and thats what I see in the database). However when I go to 'create' an object it will throw this error:

    { [SequelizeUniqueConstraintError: Validation error]
  name: 'SequelizeUniqueConstraintError',
  message: 'Validation error',
  errors: 
   [ { message: 'id must be unique',
       type: 'unique violation',
       path: 'id',
       value: '1' } ],
  fields: { id: '1' } }

The offending code:

  db.Account.create({
    email: req.body.email,
    password: req.body.password,
    allowEmail: req.body.allowEmail,
    provider: 'local',
    role: 'user'
  })

Notice ID is not specified anywhere, neither is it specified in my model definition. Also the query it generates runs fine if I run it in postgres admin:

INSERT INTO "Accounts" ("id","email","role","verifyCode","provider","cheaterScore","isBanned","allowEmail","updatedAt","createdAt") VALUES (DEFAULT,'[email protected]','user','','local',0,false,false,'2016-01-27 04:31:54.350 +00:00','2016-01-27 04:31:54.350 +00:00') RETURNING *;

Any ideas to what I could be missing here?

edit:

postgres version: 9.5 stack trace starts here: /node_modules/sequelize/lib/dialects/postgres/query.js:326

like image 914
AwDogsGo2Heaven Avatar asked Jan 27 '16 04:01

AwDogsGo2Heaven


1 Answers

Postgres has a habit of not resetting the next number in the sequence (autoincrement field) after bulk inserts. So if you're doing any pre-filling of the data in an init routine or from a SQL dump file, that's probably your issue.

Check out this post https://dba.stackexchange.com/questions/65662/postgres-how-to-insert-row-with-autoincrement-id

like image 71
Ben Polge Avatar answered Jan 03 '23 12:01

Ben Polge