-
Notifications
You must be signed in to change notification settings - Fork 11k
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
Create method on model returning wrong primary key value #27452
Comments
I believe the relevant code in the source is on line 46 of Illuminate/Database/Query/Processors/SqlServerProcessor.php 'SELECT CAST(COALESCE(SCOPE_IDENTITY(), @@IDENTITY) AS int) AS insertid' |
I can reproduce the issue, but I don't fully understand it. When I test it directly in the database, The issue with @darksaboteur You added the code in #13423, can you take a look at this? |
I have encountered this issue too when using Merge Replication. I've thrown together a branch darksaboteur@0c5a0e2 with my fixes copied to it for you to look at. I don't have time to test it now, so there may be some syntax errors or other trivial issues. Please test it and let me know if it fixes triggers too so I can create a Merge Request for it. |
Here is a shorter version of @darksaboteur's fix: staudenmeir@b4a8f81 @amestsantim Does it work for you? @darksaboteur Now that |
Closing pending author inactivity. Feel free to reply if this is still an issue. |
I'm not sure I understand why this issue is being closed. Has the bug been fixed in laravel/framework? |
@amestsantim I closed it because you didn't reply anymore. Can you please check the answers by @darksaboteur and @staudenmeir and answer their questions? |
Alright, give me a few days to patch my installation with @staudenmeir 's fix and test it. |
Closing this issue because it's inactive, already solved, old or not relevant anymore. Feel free to reply if you're still experiencing this issue. |
I am experience the same issue. Using laravel 5.8 and php 7.3 @driesvints To reproduceCreate a table
Create a model Season.php
On tinker execute That returns the next message
My question is.. Why isn't returning the season_id correctly? On the database the season_id was saved correctly. |
@skalero01 That's a different issue. You need to set |
Thanks @staudenmeir! it worked :) |
@staudenmeir @skalero01 I need to make use of
In this example, NOTE: I'm sure there is a "cause" for the value getting off, but I have no earthly idea what it is. |
@humblecoder have you tried using the fresh function?
|
@skalero01 I have. It returns I need to do some digging into what makes the PDO tick. Is it possible to reset or "re-calibrate" the results of |
@humblecoder What does "incorrect" mean? Are you also using SQL Server? |
@staudenmeir "incorrect" means the number slowly creeps off track. It's as if it has its own auto-increment mechanism. It has gotten as high as +4 off and seems it would keep climbing if I didn't manually address the issue. Yes, I'm also using SQL Server. |
I have the same issue. I had tables and inserted thee some users but I had problems with constraint. I created a dump and deleted all the tables. Then I edited dump (MySQL) and change all AUTOINCREMENT to 1 so that tables start ёшвё from 1. Created DB again. Now when I insert user it inserts it into DB with id 1 but Update After research, I found out that I use How to avoid this? |
Can reopen this issu. |
This is still an issue in 2021
|
Hey @peter-olom please see these comments on issue #32883 where you posted the same comment above: |
DriverName => "libmsodbcsql-17.2.so.0.1",
DriverODBCVer => "03.52",
DriverVer => "17.02.0000",
ExtensionVer => "5.3.0",
Description:
When creating records of a model using create the returned record will have a wrong value for primary key if the table has a trigger on it which itself creates (inserts) a record in another table.
In effect, the returned primary key value is the value of the record inserted by the trigger and not of the just created record of the model. Same behavior is observed if fresh() method is chained on the create method.
I suspect this is happening because the driver is using @@IDENTITY or SCOPE_IDENTITY whereas IDENT_CURRENT( 'table_name' ) should have been used to get the LastInsertId after the create step.
Here is the relevant section from the SQL Server 2017 documentation (https://docs.microsoft.com/en-us/sql/t-sql/functions/identity-transact-sql?view=sql-server-2017):
"@@IDENTITY and SCOPE_IDENTITY return the last identity value generated in any table in the current session. However, SCOPE_IDENTITY returns the value only within the current scope; @@IDENTITY is not limited to a specific scope.
IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the identity value generated for a specific table in any session and any scope. "
Steps To Reproduce:
Create a table (with an auto incrementing primary key) and add a trigger to it which inserts another record in another table). Then calling the create method on the model will return the just created record with the wrong value for the primary key field.
The text was updated successfully, but these errors were encountered: