You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Create method on model returning wrong primary key value #27452
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.
"@@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.
I can reproduce the issue, but I don't fully understand it.
When I test it directly in the database, SCOPE_IDENTITY() returns the correct primary key. In Laravel however, SCOPE_IDENTITY() returns null and so I receive the incorrect result of @@IDENTITY.
The issue with IDENT_CURRENT() is that it returns the last identity value generated in any session. So theoretically, a different connection could insert a new row between the INSERT and the SELECT IDENT_CURRENT() query. Then you would also get an incorrect value.
I have encountered this issue too when using Merge Replication.
I actually forgot I hadn't submitted my patches for this already.
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.
I'm not sure I understand why this issue is being closed. Has the bug been fixed in laravel/framework?
If so, great but if not, by closing the issue, we're just kicking the ball down to the next person who will create a new issue for it.
@amestsantim I closed it because you didn't reply anymore. Can you please check the answers by @darksaboteur and @staudenmeir and answer their questions?
In this example, $u->id is actually null -- not even $u->fresh() works. However, if I leave $incrementing = true then I begin to receive incorrect values for $u->id. This is borne out by trying to use DB::getPdo()->lastInsertId() which also reports the wrong value and eventually becomes more and more incorrect.
NOTE: I'm sure there is a "cause" for the value getting off, but I have no earthly idea what it is.
Currently, I mitigate the problem by re-querying for the object based on identifiable traits, but this obviously isn't the fix.
@skalero01 I have. It returns null. My guess is due to the fact that it attempts a lookup based on a bad id.
I need to do some digging into what makes the PDO tick. Is it possible to reset or "re-calibrate" the results of DB::getPdo()->lastInsertId()? 🤔 It's very strange that it gets "off track" and never recovers.
@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.
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 User::create returns id 17.
Update
After research, I found out that I use listen to log all my SQL queries. Most probably lastInsertId returns Id of log entry.
Activity
amestsantim commentedon Feb 8, 2019
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'
staudenmeir commentedon Feb 8, 2019
I can reproduce the issue, but I don't fully understand it.
When I test it directly in the database,
SCOPE_IDENTITY()returns the correct primary key. In Laravel however,SCOPE_IDENTITY()returnsnulland so I receive the incorrect result of@@IDENTITY.The issue with
IDENT_CURRENT()is that it returns the last identity value generated in any session. So theoretically, a different connection could insert a new row between theINSERTand theSELECT IDENT_CURRENT()query. Then you would also get an incorrect value.@darksaboteur You added the code in #13423, can you take a look at this?
darksaboteur commentedon Feb 10, 2019
I have encountered this issue too when using Merge Replication.
I actually forgot I hadn't submitted my patches for this already.
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.
staudenmeir commentedon Mar 10, 2019
Here is a shorter version of @darksaboteur's fix: staudenmeir@b4a8f81
@amestsantim Does it work for you?
@darksaboteur Now that
SCOPE_IDENTITY()works, do we still needCOALESCE(SCOPE_IDENTITY(), @@IDENTITY)? Shouldn'tSCOPE_IDENTITY()cover all cases?driesvints commentedon Mar 28, 2019
Closing pending author inactivity. Feel free to reply if this is still an issue.
amestsantim commentedon Mar 29, 2019
I'm not sure I understand why this issue is being closed. Has the bug been fixed in laravel/framework?
If so, great but if not, by closing the issue, we're just kicking the ball down to the next person who will create a new issue for it.
driesvints commentedon Mar 29, 2019
@amestsantim I closed it because you didn't reply anymore. Can you please check the answers by @darksaboteur and @staudenmeir and answer their questions?
amestsantim commentedon Mar 29, 2019
Alright, give me a few days to patch my installation with @staudenmeir 's fix and test it.
driesvints commentedon May 7, 2019
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.
staudenmeir commentedon Aug 4, 2019
@skalero01 That's a different issue. You need to set
public $incrementing = false;in your model.humblecoder commentedon Oct 15, 2019
@staudenmeir @skalero01 I need to make use of
idimmediately aftercreate(). Setting$incrementing = falsebreaks that ability. For example:In this example,
$u->idis actuallynull-- not even$u->fresh()works. However, if I leave$incrementing = truethen I begin to receive incorrect values for$u->id. This is borne out by trying to useDB::getPdo()->lastInsertId()which also reports the wrong value and eventually becomes more and more incorrect.NOTE: I'm sure there is a "cause" for the value getting off, but I have no earthly idea what it is.
Currently, I mitigate the problem by re-querying for the object based on identifiable traits, but this obviously isn't the fix.
humblecoder commentedon Oct 15, 2019
@skalero01 I have. It returns
null. My guess is due to the fact that it attempts a lookup based on a badid.I need to do some digging into what makes the PDO tick. Is it possible to reset or "re-calibrate" the results of
DB::getPdo()->lastInsertId()? 🤔 It's very strange that it gets "off track" and never recovers.staudenmeir commentedon Oct 15, 2019
@humblecoder What does "incorrect" mean? Are you also using SQL Server?
humblecoder commentedon Nov 7, 2019
@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.
Serhioromano commentedon Feb 28, 2020
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
User::createreturnsid17.Update
After research, I found out that I use
listento log all my SQL queries. Most probablylastInsertIdreturns Id of log entry.How to avoid this?
kadevland commentedon May 18, 2020
Can reopen this issu.
I have the same problem
I have many table with trigger so model don't take the good primary key value.
I use SQL server
peter-olom commentedon Sep 10, 2021
This is still an issue in 2021
$post = Post::create([ 'author_id' => $request->author, 'series_id' => $request->series, 'content' => $request->content, ]);After calling create method,
$post->idwill have a wrong value.rodrigopedra commentedon Sep 10, 2021
Hey @peter-olom please see these comments on issue #32883 where you posted the same comment above:
#32883 (comment)
#32883 (comment)