我有以下的數據庫,當我嘗試運行已經顯示出下面我得到說Cannot add or update a child row
錯誤的查詢。我不明白爲什麼我得到這個錯誤。ERROR 1452(23000):不能添加或更新子行
,我進入object_id
(327)存在於數據庫中。我也嘗試過與其他object_id
一樣,同樣的錯誤出現。這裏發生了什麼?
like_objects
+-------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+----------------+
| object_id | int(11) | NO | MUL | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | MUL | NULL | |
+-------------+---------+------+-----+---------+----------------+
objects
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| object_name | longtext | NO | | NULL | |
| object_desc | longtext | NO | | NULL | |
| id | int(11) | NO | PRI | NULL | auto_increment |
| likes | int(11) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
這裏的查詢和錯誤它產生:
insert into like_objects (object_id,user_id) values (327,1)
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
(`test_db`.`like_objects`, CONSTRAINT `object_id_refs_id_57f96810` FOREIGN KEY (`object_id`)
REFERENCES `objects` (`id`))
這是很奇怪的,因爲我已經使用了類似的like
邏輯另一個表,它是工作的精絕。
重要我已經從mysqldump
獲得objects
表。最初objects
表是從PhpMyAdmin創建的,並且like_objects表已由django models
請將完整性檢查(即select * from objects where id = 327')的結果添加到帖子中。 – 2012-01-13 20:54:32
另外,它可能有助於爲兩個表添加'create table'語句。 – 2012-01-13 21:02:17
@MattFenwick:由於object_desc是一個長文本,所以這個查詢的結果非常長。我無法複製整個事情,但如預期,我得到正確的元組。表中存在'id'。 – Sachin 2012-01-14 06:02:58