简体   繁体   中英

Error: The INSERT statement conflicted with the FOREIGN KEY constraint

A business user requested that I change a dropdown list to a textbox for creating records. The dropdown list is what was created by default for relating to tables in my database. As requested I made the change and replaced the dropdown list with an @Html.EditorFor element. I now get this error message when I try to create records. Is there a possible solution that can fix this error? There aren't any syntax errors that stop me from compiling my code. This error occurs at runtime.

This means that your forward thinking database designer placed a constraint in your database to stop people entering invalid values. Your application allows the user to enter invalid values, but luckily your database is stopping it.

A foreign key is how you ensure that a table only gets it's 'lookup' values from a valid list.

If you remove the constraint you will risk putting garbage into your database that is a great effort to fix.

If you start putting invalid values in this table it means things like inner joins stop working and data starts going missing in reports.

Previously with the dropdown, you were allowing the user to chose from the predefined values, ie values that were present in your lookup table to which you have put a foreign key constraint. But now as user can enter any value(which might not be present in the foreign table) thus resulting in this error.

To remove this error you can drop the constraint

ALTER TABLE <TABLE_NAME> DROP CONSTRAINT <FOREIGN_KEY_NAME>

or check the value(against the foreign key table) entered by user before inserting it.

UPDATE

If you have removed the foreign key constraint, then you'll have to insert any new (you'll have to check if data is new or old) data in the lookup table, assign it an ID and then insert this new ID in the main table to create the link between the two. NOTE: You can maintain the foreign key constraint in this case.

Need a bit more information such as tables being inserted and the foreign keys on it. Likely the text entered doesn't match that of the drop down. You would likely have to remove the foreign key. However if the design was that a numeric value was being inserted and not just a text one, The users now have a text area... where's the numeric value (I'm assuming some design here which is why I asked for tables and keys)? I'd likely do the following:

  1. Change the table structure so that the value being inserted is character based (if not already)
  2. Remove key constraint
  3. Update the existing records so the text of the lookup value is replacing the numeric values (assuming numeric design of foreign key.
  4. Drop original lookup table.
  5. Alter design so code uses distinct on the column in the remaining table for it's list of values.

The alternative is that you'd have to insert the new text value first into the lookup table, then get it's new key value then do the insert statement. I suppose this could be handled with triggers and a before update statement but the question; but unless there's a reason to keep them separate, the above numbered list seems simpler in the long run

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM