Programming

Cannot insert explicit value for identity column in table ‘table_name’ when IDENTITY_INSERT is set to OFF.

July 17, 2013

I was creating some new tables and populating them when I ran into this not so straight forward error:

Cannot insert explicit value for identity column in table 'table_name' when IDENTITY_INSERT is set to OFF.

I was confused at first, I had set the primary key to auto increment using the IDENTITY property yet it was throwing an error. I scratched my head for a few minutes before noticing my not so obvious mistake — I had confused my foreign key with the primary key.

The two fields only had a one letter difference and I had accidentally typed the name for my primary key column when I had meant to type in the other one. As a result it was trying to set the value of an identity field, which is not allowed.

After a quick correction to my insert query I was back in business.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.