-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SQLSRV driver does not support ID inserts #27778
Comments
I'm wondering if we need to create some extra commands in the SQL driver - and do something like:
Or even better:
which would wrap the insert with |
I'll need someone else using SQLSRV to confirm this. |
Use DB::unprepared('SET IDENTITY_INSERT test_table ON');
DB::table('test_table')->insert(['id' => 1, 'name' => 'example']);
DB::unprepared('SET IDENTITY_INSERT test_table OFF'); The difference is that |
@staudenmeir - you are a genius - that resolves it. So then the next question is - how do we document this moving forward? I'll update the 2-3 forum threads/SO topics I found on Google about this issue, but should there be something in the docs? edit: p.s. could you explain why that solves it? |
I've added the explanation to my previous post. We could append the |
Feel free to send in a pr to the docs for this! |
If you're getting an Assertion Error, try this workaround: DB::unprepared('SELECT 1; SET IDENTITY_INSERT test_table ON');
DB::table('articles')->insert(['id' => $request->input('article_id'), 'title' => $request->input('title'), 'body'=> $request->input('body')]);
DB::unprepared('SELECT 1; SET IDENTITY_INSERT test_table OFF');
I struggled with a number of approaches to work around this but ultimately what I found was that by adding a trivial SELECT before the Please let me know if there's a more elegant solution. It's possible that my particular issue was a driver quirk or DB setting, but I'm using the same Docker SQL Server 2019 configuration as on other environments that didn't have this issue. |
Description:
As part of an enterprise project, my client needs to use MSSQL. Trying to insert a seed with a specific id gives an error:
This is expected behavior, as MSSQL works a bit differently to most other databases, and does not allow IDs to be inserted by default. The method to solve is to turn
IDENTITY_INSERT
toON
for the table, insert the record, then turn itOFF
again:Except this doesnt work, and MSSQL still thinks the
IDENTITY_INSERT
isOFF
. If you run the above as native SQL queries - it works, so the problem is how Laravel is handling it.Technically, we should be able to wrap the whole thing in a transaction, but that doesnt work either:
Googling around, I found this SO thread which people have the same issue. There is a manual workaround, but the answer given there is not practical for me or suitable for long term use.
I only really need the
id
inserting forseeding
and a few edge cases, - but this seems like a bug to me?The text was updated successfully, but these errors were encountered: