mlila_p
mlila_p

Reputation: 131

Cannot add a foreign key constraint MySQL

I have a table named "countries" and another table named "country_continents" in my DB. I want to make my continent_id column in countries , a foreign key referencing the id of country_continents, but I am getting an error message. This is the SQL to create the foreign key and the error:

ALTER TABLE countries
    ADD CONSTRAINT fk_continent_id
    FOREIGN KEY (continent_id)
    REFERENCES country_continents(id);

ERROR: #1215 - cannot add foreign key constraint

At first, I was getting the :

"Error: relational features are disabled"

so I ran the command ALTER TABLE countries ENGINE=InnoDB; and ALTER TABLE country_continents ENGINE=InnoDB; but now I'm getting the #1215 error.

This is the Structure for "country_continents": enter image description here

This is the Structure for "countries": enter image description here

Any ideas on whats happening? Thanks in advance.

Upvotes: 0

Views: 164

Answers (1)

Atika
Atika

Reputation: 1067

I think the error is due to:

in your countries table, continent_id is not unsigned. Edit that and tell me if it worked

Upvotes: 1

Related Questions