I am trying to create one to many relation using one table. Is this possible?
create table user(id int primary key auto_increment not null,
created_by int default null
)ENGINE=INNODB;
alter table user add foreign key (created_by) references user(id) ON DELETE SET NULL ON UPDATE CASCADE;
insert into user (id) VALUES(1);
insert into user (id, created_by) VALUES (2,1);
Now when I delete user with id=1 the value of created_by automaticaly change to NULL as I expected.
But when I change id of the user with id=1 I get this error
mysql> update user set id=2 where id=1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`jrt`.`user`, CONSTRAINT `user_ibfk_1` FOREIGN KEY (`created_by`) REFERENCES `user` (`id`) ON DELETE SET NULL ON UPDATE CASCADE)
How can I fix this? Affter this update I want the column created_by to be cascade changed.