我正在使用Yii框架并在尝试插入表时对数据库产生以下Yii错误 - 我可以看到user_id为'5702157058'的用户存在于我的'myuser'表中,因此不确定为什么我收到这个错误了?
<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'
答案 0 :(得分:0)
请检查以下几点,
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?
主要是您的外键设置有任何问题。
答案 1 :(得分:0)
检查'users'和'organisation_news_read'之间的关系肯定会出现问题。 试试这个来验证是否存在。
SELECT * FROM users WHERE id = 5702157058;
这应该找到一排。
我认为你的表就像这些。 (顺便说一下这个例子是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
)
希望这会有所帮助