Ok, so my problem is inserting the initial "base" record into my classes table. Here is the code along with the received error. It looks ok from what I see, and this error has been killing me. I want to keep the foreign key constraints. Any suggestions would be useful. Thanks.
Code:
drop table if exists classes;
create table classes
(
id int unsigned not null auto_increment,
name varchar(50) not null,
description tinytext,
parent_class_id int unsigned not null default 0,
index(parent_class_id),
foreign key(parent_class_id) references classes(id),
primary key(id)
)ENGINE=InnoDB;
/*
This would bee the "base" class that all other classes inherit from by default.
The problem is creating this first record with the foreign key value referring to the same primary key value, and both being created at the same time.
*/
insert into classes
values(0,'base','description blah blah....',0);
/*
These are the following results
Query OK, 0 rows affected (0.34 sec)
Query OK, 0 rows affected (0.19 sec)
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`proj`.`classes`, CONSTRAINT `classes_ibfk_1` FOREIGN KEY (`parent_class_id`) REFERENCES `classes` (`id`))
*/