Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have the following two tables with a foreign key on status_id:

mysql> describe usr_cookbook;
+-----------+-----------+------+-----+-------------------+-------+
| Field     | Type      | Null | Key | Default           | Extra |
+-----------+-----------+------+-----+-------------------+-------+
| user_id   | int(11)   | NO   | PRI | NULL              |       |
| recipe_id | int(11)   | NO   | PRI | NULL              |       |
| status_id | int(11)   | NO   | MUL | NULL              |       |
| added_ts  | timestamp | NO   |     | CURRENT_TIMESTAMP |       |
+-----------+-----------+------+-----+-------------------+-------+

mysql> describe usr_cookbook_status;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| status_id | int(11)     | NO   | PRI | NULL    |       |
| name      | varchar(45) | NO   |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+

There's two rows in usr_cookbook_status:

mysql> select * from usr_cookbook_status;
+-----------+-----------+
| status_id | name      |
+-----------+-----------+
|         1 | Try Soon  |
|         2 | Favorites |
+-----------+-----------+

So I should be good to insert into usr_cookbook right?

mysql> insert into usr_cookbook (user_id, recipe_id, status_id) values (3, 5, 1);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint 
fails (`tomato`.`usr_cookbook`, CONSTRAINT `fk_cookbook_status` FOREIGN KEY 
(`status_id`)
REFERENCES `usr_cookbook_status` (`status_id`) ON DELETE NO ACTION ON UPDATE
NO ACTION)

Why the constraint violation? The status_id I'm using (1) exists in the parent table. I'm sure there's something simple I'm missing here...

share|improve this question
2  
please show us your DDL rather than the "describe" commands. –  Brian Ballsun-Stanton Jan 11 '11 at 5:20

1 Answer 1

up vote 2 down vote accepted

When I looked at the DDL per Brian's suggestion the problem immediately became clear: The usr_cookbook_status table was using MyISAM engine. I switched it to InnoDB and everything works now.

share|improve this answer
    
And that's called rubber-ducking. Glad you figured it out! –  jcolebrand Jan 11 '11 at 14:18
    
@jcolebrand: I've never heard that term before, had to look it up: en.wikipedia.org/wiki/Rubber_duck_debugging –  BenV Jan 11 '11 at 16:45
    
And more than once I've typed out a complete question in stackoverflow and then go "oh." –  BenV Jan 11 '11 at 16:46
    

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.