php - MySQL insert with undefined foreign key -
i have table such schema (simplified):
create table folders( id int(11) unsigned not null auto_increment, parent int(11) unsigned not null, primary key (id), index fk_folders_folders_id (parent), constraint fk_folders_folders_id foreign key (parent) references folders (id) on delete cascade on update cascade )
1 folder can have many subfolders , can belongs 1 folder. if it's root folder parent contain it's own id.
the problem is: how can create root folder? id auto_increment , can after inserting row cant insert row while parent not defined. recursion...
you can remove not null attribute parent field, way can have root. of course, in case have garantee folder tree cosistency in code , not through database. mysql reference manual not advise users this:
you advised use foreign keys reference unique , not null keys. http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
but it's you.
Comments
Post a Comment