【发布时间】:2016-12-01 15:05:37
【问题描述】:
我正在尝试通过使用 Sequelize 和 Postgresql 显式创建的联结表添加多对多关系。
关系两边的表是这样关联的:
Shop.belongsToMany(models.user, {through: 'visits' })
User.belongsToMany(models.shop, {through: 'visits' })
访问联结表主键定义如下:
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true // Automatically gets converted to SERIAL for postgres
}
当我尝试插入访问时,我收到以下错误:
ERROR: duplicate key value violates unique constraint "visits_shopId_userId_key"
DETAIL: Key ("shopId", "userId")=(1, 12) already exists.
在执行 pg_dump 后,我尝试通过在模型中添加 constraint: false 来移除复合键约束,但仍然出现错误。
(我在调试过程中删除了表并重新同步了几次)
【问题讨论】: