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
最佳答案
Postgres 习惯于在批量插入后不重置序列中的下一个数字(自动增量字段)。因此,如果您在 init 例程中或从 SQL 转储文件中对数据进行任何预填充,那可能就是您的问题。
查看这篇文章 https://dba.stackexchange.com/questions/65662/postgres-how-to-insert-row-with-autoincrement-id
关于node.js - Sequelize 在创建时抛出 'id must be unique',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35029052/