我试图将一列设置为外键,但出现错误
Unhandled rejection SequelizeForeignKeyConstraintError: The MERGE statement conflicted with the FOREIGN KEY constraint "FK__Wishlists__produ__03C67B1A". The conflict occurred in database "database", table "dbo.posts", column 'productid'.
productid
中的外键名称是 cart_table
,product_table
中的主键是 productid
Cart.belongsTo(Product, {foreignKey: 'productid', targetKey: 'productid'});
这甚至是正确的方法吗?
设想:
我会通过将 car_table 与 product_table 连接来操作数据,以便结果是:
购物车_产品表 :
username: alex
cart_table.productid: 01
product_table.productid: 01
productName: Shampoo
productPrice: 12
最佳答案
正如评论中所讨论的,您在购物车中有数据,因为您收到了错误。
Cart.belongsTo(Product, {foreignKey: 'productid', targetKey: 'productid'});
用此更改此代码。
Cart.belongsTo(Product, {
foreignKey: 'productid',
targetKey: 'productid',
defaultValue : 0
});
你只需要添加默认值
关于sql - Node.js 和 SQL Server Sequelize 连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50980338/