Reputation: 31
I tried to set a column as foreign key but I get the error
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'.
The foreign key name is productid
in cart_table
and in the product_table
the primary key is productid
Cart.belongsTo(Product, {foreignKey: 'productid', targetKey: 'productid'});
Is this even the correct way?
Scenario:
I would to manipulate the data by joining the cart_table with the product_table so that result would be :
cart_product_table:
username: alex
cart_table.productid: 01
product_table.productid: 01
productName: Shampoo
productPrice: 12
Upvotes: 0
Views: 365
Reputation: 470
As discussed in the comment you have data in the cart because of that you got the error.
Cart.belongsTo(Product, {foreignKey: 'productid', targetKey: 'productid'});
change this code with this.
Cart.belongsTo(Product, {
foreignKey: 'productid',
targetKey: 'productid',
defaultValue : 0
});
You have to just add default value
Upvotes: 1
Reputation: 429
Based on your scenario, your are doing one to one mapping of Product
to Cart
.
You did association of Cart to Product as,
Cart.belongsTo(Product, {
foreignKey: 'productid',
targetKey: 'productid'
});
Similarly you have to associate Product to Cart too.
Product.belongsTo(Cart, {
foreignKey: 'productid',
sourceKey: 'productid'
});
Upvotes: 0