dave
dave

Reputation: 15459

mysql error "cannot add foreign key constraint"

Here is the mysql query:

ALTER TABLE wp_juices_members
ADD CONSTRAINT fk_juiceid FOREIGN KEY(juice_id) REFERENCES wp_juices(id)

All the tables, fields exist with the correct names as specified in the above query, and i still get this error:

"cannot add foreign key constraint"

Here are the DESC for the two tables:

wp_juices_members:

Field       Type    Null    Key Default Extra   
id          int(11) unsigned    NO  PRI NULL    auto_increment
member_id   int(11) YES     NULL    
juice_id    int(11)

wp_juices:

Field   Type    Null    Key Default Extra   
id  int(11) unsigned    NO  PRI NULL    auto_increment
name    varchar(255)    NO      NULL    

UPDATE: Actually I figured it out, the fields being matched have to be same type, and adjectives like unsigned or not not null etc. Thanks everyone for help.

Upvotes: 1

Views: 96

Answers (1)

william.eyidi
william.eyidi

Reputation: 2365

You might want to add a description of the table you are using just DESC table_name then update your question.

the most common reason will be that the number of digit in each of your column you want to link with a reference is different maybe INT(11) UNSIGNED and INT(11) SIGNED will create a big difference.

Upvotes: 1

Related Questions