tags:

views:

40

answers:

1

I am trying to add a self relation in an existing Innodb table here is table structure

Table person

   person_id int (10) primary key not null auto increment,

   parent_id int (10) NULL default null,

   name varchar(30)

When I use this command

ALTER TABLE `person` ADD FOREIGN KEY ( `parent_id` ) REFERENCES `person` (`person_id`) ON DELETE RESTRICT ON UPDATE RESTRICT ;

I get the error data type mismatch. I think this could be due to null values in parent_id. Is there any way to skip this check?

Thanks

+2  A: 

person_id and parent_id need to be the exact same data type. For example, if person_id is INT UNSIGNED and parent_id is INT, then you can't create the foreign key.

Run this command and compare the data types of the two columns:

SHOW CREATE TABLE `person`\G
Ike Walker
There was unsigned different in both :)
jason