Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INSERT statement conflicted with COLUMN FOREIGN KEY

I have a FK in my table that looks like this:

FK

Clearly, I should be able to insert NULL into this column as well as any value that exists in the parent table.

But when I try to insert NULL, I get the following error:

The INSERT statement conflicted with the FOREIGN KEY constraint "users_fk". The conflict occurred in database "mydatabase", table "dbo.country", column 'country_id'.

like image 518
Adil Malik Avatar asked Apr 29 '12 14:04

Adil Malik


1 Answers

You are absolutely right - you should be able to insert NULL into this column.

Are you 500% sure that you are? The error message says something else - it appears as if you're inserting an invalid value.

Could it be that you have

(a) another line in your script that inserts additional (invalid) values?

Or

(b) do you happen to have a trigger on this table that does something invalid?

Update: (based on your comment)
If you're not specifying the column in the INSERT statement - then maybe an invalid default value is defined on that column? Something like a default of 0 or something.

If you do have a default value - that default value will be inserted (and not NULL, as you seem to be expecting)

like image 55
marc_s Avatar answered Oct 12 '22 10:10

marc_s