简体   繁体   English

无法删除或更新父行:外键约束失败 SQL 错误

[英]Cannot delete or update a parent row: a foreign key constraint fails SQL error

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.我正在尝试使用 y 前端(前端在 reactjs 中)上的删除按钮删除产品行,但它会在 python SQL 服务器上引发此错误消息。 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) 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 product_dao.py 文件代码

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"

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

Postman Request: Postman 请求: 在此处输入图像描述

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您的错误是不言自明的,您有外键引用,您必须先删除/更新子表中的值,然后再删除父表,或者使用ON DELETE/UPDATE CASCADE自动执行相同操作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IntegrityError: (1451, '无法删除或更新父行:外键约束失败 (..)) - IntegrityError: (1451, 'Cannot delete or update a parent row: a foreign key constraint fails (..)) (pymysql.err.IntegrityError)(1451,'无法删除或更新父行:外键约束失败...') - (pymysql.err.IntegrityError) (1451, 'Cannot delete or update a parent row: a foreign key constraint fails…') in Flask 错误1452(23000):无法添加或更新子行:外键约束失败我无法修复 - ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails That I can't fix Python Sqlalchemy mysql“无法添加或更新子行:外键约束失败” - Python Sqlalchemy mysql “Cannot add or update a child row: a foreign key constraint fails” 无法添加或更新子行:Django生成的MySQL表上的外键约束失败 - Cannot add or update a child row: a foreign key constraint fails on a Django generated MySQL table IntegrityError(1452,'无法添加或更新子行:外键约束失败) - IntegrityError (1452, 'Cannot add or update a child row: a foreign key constraint fails) Python 插入 MySQL:无法记录价格 1452 (23000):无法添加或更新子行:外键约束失败 - Python insert to MySQL : Failed to record Price 1452 (23000): Cannot add or update a child row: a foreign key constraint fails 在peewee中创建新模型实例时,如何修复“无法添加或更新子行:外键约束失败” - How to fix 'Cannot add or update a child row: a foreign key constraint fails' when creating new model instance in peewee 使用SQLAlchemy删除级联外键约束错误 - Delete cascade foreign key constraint error with SQLAlchemy Django 提交行时外键约束错误 - Django Foreign key constraint error on submitting a row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM