Reputation: 6164
I've got a Laravel application (5.1.28) which I have running on a development server and production server.
dev: php 5.6.13 mysql 5.6.19
prod: php 5.6.16 mysql 5.5.27
mysql table:
CREATE TABLE
testquestions
(
id
bigint(20) UNSIGNED NOT NULL,
testquestiongroup_id
bigint(20) NOT NULL,
description
varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
type
varchar(255) COLLATE utf8_unicode_ci NOT NULL,
created_at
timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
updated_at
timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
And the following php code;
$question = new Question();
$question->testquestiongroup_id = $questiongroup->id;
$question->save();
The code on my dev and prod environment are the same. Als the database content are the same, copied from prod to dev.
When I run this code my $question object will get a wrong ID back from mysql(?). When I check MySQL there is a new record with a (good) ID, but this ID isn't returned to my object. It get the same wrong ID over and over again. The wrong ID is from an object added 4 months ago.
This problem doesn't occur on my dev environment.
Is there some bug in MySQL or am I doing something wrong? Thanks
UPDATE 12febr
The problem get keeping weirder! I tried the following code after inserting my code ($question->save())
var_dump($question);
var_dump(\DB::getPdo()->lastInsertId());
My question object gives a different ID ($question->id) than \DB::getPdo()->lastInsertId(). The returned ID from the $question object stays the wrong ID. The lastInsertId function returns the right ID!
Does it matter that the field is a BIGINT(20) ?
Upvotes: 1
Views: 1677
Reputation: 17567
I have some similar problem and finally solved it. Not the same, but it is also about laravel-model-gets-wrong-id-after-saving, so I hope I can post it here to let someone having my problem solve it.
My solution: Check about the DB::getPdo()->lastInsertId()
. If it is also wrong, then check whether you have some sql insert statement inside DB::listen
callback.
I am a fool for I tried to log my sql statement inside DB::listen by using an insert
statement. This is the problem.
Upvotes: 3
Reputation: 1345
I had some problems with id's in laravel/eloquent.
Setting the incrementing variable to false in the specific model helped me most of the time.
Perhaps this will help you too.
Upvotes: 0