简体   繁体   English

错误1452(23000):无法添加或更新子行:外键约束失败我无法修复

[英]ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails That I can't fix

I am getting this error : 我收到此错误:

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint 
fails (\`GTFS\`.\`#sql-37d_16\`, CONSTRAINT \`#sql-37d_16_ibfk_1\` FOREIGN KEY 
(\`service_id\`) REFERENCES \`calendar\` (\`service_id\`))

I'm trying to create my database tables with python , this is what i've tried : 我正在尝试使用python创建数据库表,这是我尝试过的:

mycursor.execute("CREATE TABLE IF NOT EXISTS routes(route_id  varchar(3) PRIMARY KEY, agency_id varchar(2) ,route_short_name varchar(20) ,route_long_name varchar(50) ,route_desc varchar(30) ,route_type varchar(30) ,route_url varchar(30) ,route_color varchar(30) ,route_text_color varchar(30)) ")

mycursor.execute("CREATE TABLE IF NOT EXISTS calendar (service_id varchar(4) PRIMARY KEY,monday varchar(4) ,tuesday varchar(4) ,wednesday varchar(4) ,thursday varchar(4) ,friday  varchar(4) ,saturday varchar(4) ,sunday varchar(4) ,start_date varchar(8) ,end_date varchar(8)) ")

mycursor.execute("CREATE TABLE IF NOT EXISTS trips (route_id varchar(3) ,service_id varchar(4) ,trip_id varchar(6) PRIMARY KEY,trip_headsign varchar(20) ,trip_short_name varchar(3) ,direction_id varchar(1) ,block_id varchar(1) ,shape_id varchar(5) )")

mycursor.execute("ALTER TABLE trips ADD FOREIGN KEY (service_id) REFERENCES calendar(service_id); ")

mycursor.execute("ALTER TABLE trips ADD FOREIGN KEY (route_id) REFERENCES routes(route_id); ")

I'm expecting to insert into my table some data provided in a list but i always get this error in my terminal 我期望将列表中提供的一些数据插入表中,但是我总是在终端中收到此错误

Because some values for trips.service_id column don't exist in the parent( calendar ) table for the common column service_id . 因为trips.service_id列的某些值在公共列service_id的父表( calendar )表中不存在。

You may try to insert those values by such a insert statement : 您可以尝试通过这样的插入语句插入这些值:

insert into calendar(...,service_id,...) 
select ...,service_id,... 
  from trips t
 where not exists ( select 0 from calendar where service_id=t.service_id );

before adding the constraint by 在添加约束之前

ALTER TABLE trips ADD FOREIGN KEY (service_id) REFERENCES calendar(service_id);

暂无
暂无

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

相关问题 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 IntegrityError(1452,'无法添加或更新子行:外键约束失败) - IntegrityError (1452, 'Cannot add or update a child row: a foreign key constraint fails) django.db.utils.IntegrityError: (1452, '无法添加或更新子行:外键约束失败 - django.db.utils.IntegrityError: (1452, '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 错误1452'无法添加或更新子行' - Error 1452 'Cannot add or update a child row' 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 无法删除或更新父行:外键约束失败 SQL 错误 - Cannot delete or update a parent row: a foreign key constraint fails SQL error 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM