6

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'.

4

1 回答 1

15

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)

于 2012-04-29T20:12:16.400 回答