简体   繁体   English

违反完整性约束:1452无法添加或更新子行:外键约束失败

[英]Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

I am using the Yii framework and having the following Yii error with the database when attempting an insert into a table - I can see that the user with the user_id of '5702157058' exists in my 'myuser' table so not sure why I am getting this error? 我正在使用Yii框架,并且尝试在表中插入时数据库出现以下Yii错误-我可以看到我的“ myuser”表中存在user_id为'5702157058'的用户,所以不确定为什么这个错误?

<h1>CDbException</h1>
<p>CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation:       
1452 Cannot add or update a child row: a foreign key constraint fails (`mydb`.`organisation_news_read`, 
CONSTRAINT `FK_organisation_news_read_myuser` FOREIGN KEY (`user_id`) REFERENCES `myuser_`
(`user_id`) ON DELETE CASCADE ON UPDATE NO ACTION). The SQL statement executed was: 
INSERT INTO `organisation_news_read` (`news_id`, `user_id`) VALUES (:yp0, :yp1). 
Bound with :yp0=287, :yp1='5702157058'

Please check the following points, 请检查以下几点,

1. First check your foreign key field name and its spelling are same.
2. then check your model relation function
3. check your table field are same
4. check the DB engine is it INNODB?

mostly your foreign key setup have any issues. 大多数情况下,您的外键设置有任何问题。

Check the relationship between 'users' and 'organisation_news_read' surely this is the problem. 一定要检查“用户”和“ organization_news_read”之间的关系。 Try this to validate if exists. 尝试此操作以验证是否存在。

SELECT * FROM users WHERE id = 5702157058;

This should find a row. 这应该找到一行。

I think your tables are like these. 我认为您的桌子就是这样。 (this example is in postgres by the way) (顺便说一下,这个例子在postgres中)

CREATE TABLE users
(
  id serial NOT NULL,
  name character varying(50),
  last_name character varying(50),
  CONSTRAINT pk_users PRIMARY KEY (id)
)



CREATE TABLE organisation_news_read
(
  id serial NOT NULL,
  users_id integer,
  CONSTRAINT pk_organisation PRIMARY KEY (id),
  CONSTRAINT fk_organisation_vs_users FOREIGN KEY (users_id)
      REFERENCES users (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

hope this helps 希望这可以帮助

暂无
暂无

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

相关问题 SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败 - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails 违反完整性约束:1452无法添加或更新子行:外键约束失败 - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Laravel 5:违反完整性约束:1452 无法添加或更新子行:外键约束失败 - Laravel 5: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Laravel 5.2 - 违反完整性约束:1452 无法添加或更新子行:外键约束失败 - Laravel 5.2 - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Laravel:SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败 - Laravel: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败 - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:Laravel 5中的外键约束失败 - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails in Laravel 5 Laravel 迁移 - 违反完整性约束:1452 无法添加或更新子行:外键约束失败 - Laravel migration - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Laravel - 完整性约束违规:1452 无法添加或更新子行:外键约束失败 - Laravel - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails SQLSTATE [23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败 - Laravel - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - Laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM