conan
conan

Reputation: 1337

How come I cannot insert data mysql after I add foreign key?

After I add foreign key into my table, I cannot insert data into it. for example, Table Fruit with id, order_id , amount. I add foreign key Fruit (order_id) reference another table. After that, I cannot INSERT INTO amount or other columns, but I can only INSERT INTO the foreign key order_table. How can I solve this problem? Appreciate.

Upvotes: 0

Views: 944

Answers (3)

anam wazir
anam wazir

Reputation: 11

when you define a foreign key in a table .after that when you will go to insert values into that table there will be drop down menu in front of foreign key . so you have to choose a vale either 1 or 2 etc then data will be inserted(remember firstly you have to insert data into that table from which you are bringing foreign key so that there may be some value that after ward you browse that in another table in drop down)

Upvotes: 1

John M Fecko
John M Fecko

Reputation: 105

I think you may have your foreign key relationship configured backwards. When you are trying to insert into the table Fruit, MySQL is expecting order_id to be a valid record from the order_table.

I would expect that the table Fruit would reference to multiple orders, so the order_table should have a foreign key relationship to the id column from the Fruit table.

Upvotes: 0

hamed
hamed

Reputation: 8043

Your Fruit table has a column that referenced to another table. So you need to define order_id in each Fruit row. Fruit row can not has empty value in order_id column. Try to insert a row contains order_id into Fruit table.

Upvotes: 0

Related Questions