Sequelize在创建时抛出“id必须是唯一的”

时间:2016-01-27 04:43:12

标签: node.js sequelize.js

Sequelize库的新手。根据我的理解,'id'是由sequelize自动创建的(这就是我在数据库中看到的)。但是,当我去“创建”一个对象时,它会抛出这个错误:

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

违规代码:

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

通知ID未在任何地方指定,也未在我的模型定义中指定。如果我在postgres admin中运行它,它生成的查询运行正常:

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

对于我在这里可能缺少的任何想法?

编辑:

postgres版本:9.5 堆栈跟踪从这里开始: /node_modules/sequelize/lib/dialects/postgres/query.js:326

1 个答案:

答案 0 :(得分:5)

Postgres习惯在批量插入后不重置序列中的下一个数字(自动增量字段)。因此,如果您在init例程或SQL转储文件中预先填充数据,那可能就是您的问题。

查看此帖子https://dba.stackexchange.com/questions/65662/postgres-how-to-insert-row-with-autoincrement-id