Azeem
Azeem

Reputation: 145

Unable to create foreign key constraint?

I'm having two tables: Persons and person_config having the below schemas: -

/*Table: persons*/
------------------

/*Column Information*/
----------------------

Field        Type               Collation           Null    Key     Default              Extra           Privileges                       Comment  
-----------  -----------------  ------------------  ------  ------  -------------------  --------------  -------------------------------  ---------
person_id    int(100) unsigned  (NULL)              NO      PRI     (NULL)               auto_increment  select,insert,update,references           
person_name  varchar(25)        utf8mb4_general_ci  YES             (NULL)                               select,insert,update,references           
added_date   date               (NULL)              YES             current_timestamp()                  select,insert,update,references           
added_logon  varchar(10)        utf8mb4_general_ci  YES             Admin                                select,insert,update,references           

/*Index Information*/
---------------------

Table    Non_unique  Key_name  Seq_in_index  Column_name  Collation  Cardinality  Sub_part  Packed  Null    Index_type  Comment  Index_comment  
-------  ----------  --------  ------------  -----------  ---------  -----------  --------  ------  ------  ----------  -------  ---------------
persons           0  PRIMARY              1  person_id    A                  992    (NULL)  (NULL)          BTREE 

and

/*Table: person_config*/
------------------------

/*Column Information*/
----------------------

Field        Type               Collation           Null    Key     Default              Extra           Privileges                       Comment  
-----------  -----------------  ------------------  ------  ------  -------------------  --------------  -------------------------------  ---------
config_id    int(100) unsigned  (NULL)              NO      PRI     (NULL)               auto_increment  select,insert,update,references           
person_id    int(100)           (NULL)              NO      PRI     (NULL)                               select,insert,update,references           
config       varchar(25)        utf8mb4_general_ci  NO              (NULL)                               select,insert,update,references           
value        varchar(25)        utf8mb4_general_ci  NO              (NULL)                               select,insert,update,references           
added_date   timestamp          (NULL)              NO              current_timestamp()                  select,insert,update,references           
added_logon  varchar(25)        utf8mb4_general_ci  YES             Admin                                select,insert,update,references           

/*Index Information*/
---------------------

Table          Non_unique  Key_name  Seq_in_index  Column_name  Collation  Cardinality  Sub_part  Packed  Null    Index_type  Comment  Index_comment  
-------------  ----------  --------  ------------  -----------  ---------  -----------  --------  ------  ------  ----------  -------  ---------------
person_config           0  PRIMARY              1  config_id    A                    1    (NULL)  (NULL)          BTREE                               
person_config           0  PRIMARY              2  person_id    A                    1    (NULL)  (NULL)          BTREE        

I want to make something like ADD Person_Config(person_id) refers to persons(person_id). Kindly find the below query:

Alter table `movies`.`person_config`  
  add foreign key (`person_id`) references `movies`.`persons`(`person_id`)

Im getting the below error:

Can't create table movies.person_config (errno: 150 "Foreign key constraint is incorrectly formed")

Kindlye guide me on this, what is wrong with this.

Upvotes: 0

Views: 28

Answers (2)

Mike Araya
Mike Araya

Reputation: 164

from what i see you have made person_id on person_config primary key plus person_id on person table is unsigned int but int on person_config foreign key type must be exactly the same on parent and child table so remove person_id primary key constraint and also make it unsigned int and try again.

Upvotes: 1

Sam
Sam

Reputation: 632

Your person_config has person_id as a primary key. You need to remove it before adding foreign key relation for that column.

Upvotes: 1

Related Questions