question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

It may be that the connection pool uses different connections. After execution SET identity_insert [Table] ON, it explicitly inserts the identity column reports an error, even request in the same transaction.


  it('mssql -> trans identity_insert on', async () => {
    dbConfig.server = dbConfig.host
    const pool = await require('mssql').connect(dbConfig)
    const trans = pool.transaction()
    await trans.begin()
    try {
      const req = trans.request()
      await req.query('SET identity_insert [Items] ON')
      const req2 = trans.request()
      // id is the identity column
      await req2.query("INSERT INTO [Items](Fid, Fname) VALUES(1, 'aName')")
      // throw error: Cannot insert explicit value for identity column in table 'Department' when IDENTITY_INSERT is set to OFF.
      await trans.commit()
    } catch (ex) {
      await trans.rollback()
      throw ex
    }
  })

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
jacargentinacommented, Jun 14, 2020

@dhensby I’ve refactored to this, and it works!

await req.query('SET identity_insert [Items] ON; INSERT items(Oid....) VALUES(1, ....)')
0reactions
dhensbycommented, Jun 14, 2020

You would need to use a transaction. There is no guarantee that the pool will release the same connection both times so each query will be run on different connections.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql - Cannot insert explicit value for identity column in table ...
The PRIMARY KEY "ID" MUST BE PRESENT from the "INSERT INTO" Statements as long as the ID value does not already exist: If...
Read more >
How to Fix the Error 'Cannot insert explicit value for identity ...
Error 1: Set identity_insert OFF ... In the first case, we will insert data into the table with the “IDENTITY INSERT” set to...
Read more >
Little SQL Server Tricks: How to Fix “Cannot insert explicit ...
Fore once the error message says exactly what the problem is. In this case, your INSERT statement fails because IDENTITY_INSERT is set to...
Read more >
SqlException: Cannot insert explicit value for identity column
This error means you have an identity column in the table, and you're trying to set a value for it. When you have...
Read more >
Cannot insert explicit value for identity column in table ...
this is because you are passing some value in a column which is set as identity (auto-increment). hope it helps. otherwise, please send...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found