I'm trying to delete the product row using the delete button on y frontend(frontend is in reactjs) but it throws this error message on the python SQL server. ysql.connector.errors.IntegrityError: 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (
grocery_store .
order_details , CONSTRAINT
fk_product_id FOREIGN KEY (
product_id ) REFERENCES
products (
product_id ) ON UPDATE RESTRICT)
delete route code:
@app.route('/delete', methods=['POST'])
def delete_product():
return_id = products_dao.delete_product(connection, request.json["product_id"])
response = jsonify({
'product_id': return_id
})
response.headers.add('Access-Control-Allow-Origin', '*')
return return_id
product_dao.py file code
def delete_product(connection, product_id):
cursor = connection.cursor()
query = ("DELETE FROM products where product_id={}".format(product_id))
cursor.execute(query)
connection.commit()
return "product deleted"
Your error is self explanatory, you have foreign key references you have to either first delete/update values in the child table and then to parent or use ON DELETE/UPDATE CASCADE
which will automatically do the same
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.