Ver Mensaje Individual
  #1 (permalink)  
Antiguo 12/11/2014, 14:31
themarkes
 
Fecha de Ingreso: abril-2011
Mensajes: 11
Antigüedad: 12 años, 11 meses
Puntos: 1
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails

Hola,

A ver si alguien me puede ayudar con esto. Tengo dos tablas, 'images' y 'entries'. La tabla images tiene un campo llamado entry_id que referencia al id de la tabla 'entries'.

Al hacer un insert en la tabla images me da un error 1452, he comprobado que el valor que intento insertar en el campo 'entry_id' existe en la tabla entries:

mysql> select * from entries where id=17;
+----+---------+-------+------+--------+-----------+---------------------+---------------------+
| id | user_id | title | text | public | published | created | modified |
+----+---------+-------+------+--------+-----------+---------------------+---------------------+
| 17 | 1 | NULL | NULL | 0 | 0 | 2014-11-12 19:35:40 | 2014-11-12 19:35:40 |
+----+---------+-------+------+--------+-----------+---------------------+---------------------+

Pero aún así me da error:

mysql> INSERT INTO `diary`.`images` (`title`, `description`, `entry_id`, `width`, `height`, `extension`, `user_id`, `modified`, `created`) VALUES (NULL, NULL, 17, 176, 231, 'gif', 1, '2014-11-12 19:35:40', '2014-11-12 19:35:40');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`diary`.`images`, CONSTRAINT `fk_images_entries1` FOREIGN KEY (`entry_id`) REFERENCES `entries` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)

Los dos campos son de tipo INT,

mysql> desc entries;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |

mysql> desc images;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | MUL | NULL | |
| entry_id | int(11) | YES | MUL | NULL | |

no tengo ni idea de porque puede estar fallando.

Muchas gracias y un saludo
Ángel