Laravel-debugbar: Since v3.2.1 model ids are 0 after create()

Created on 19 Nov 2018  Â·  14Comments  Â·  Source: barryvdh/laravel-debugbar

We experienced a very strange bug today, were after an Model create call the id of the returning model was always just 0. It turns out that the debugbar causes the problem, more precisely after the changes of commit #0e4c5fad the problem occurs.

dd(Setting::query()->create([
     'name' => 'test' ,
     ...
]));

Result:

#attributes: array:13 [â–¼
    "name" => "test"
    ...
    "id" => 0
]

Versions:
Laravel: 5.7.13
Debugbar: 3.2.1

stale

Most helpful comment

Quick question: What does the (...) for MySQL 5.6.3+ means in the comment that was added on the config file? Is this behavior not the same with PostgreSQL or SQLite?

All 14 comments

Is there no one else who has this problem as well? I found that this only happens on web access, not on the console!

Update:
This only happens with the config option 'explain' true for insert in query collection.

I'm having this problem as well, just confirmed it was debug-bar by reinstalling all packages in my project one by one.

Turning the config option explain to false did fix it for me. Thanks @bastian-schur

Feel like I had this enabled for some time before the issue started but I can't say for sure.

Laravel: 5.7.22
Debugbar: 3.2.1

Thanks, can you submit a pr to revert the change?

Not really why that would happen though, the explain is a separate query?

I think it's not right to do a revert. The commit resolved a regex issue, so the explain function never ran before. This has never been noticed before, because explain never ran.

Why the Explain caused a problem I could not find out yet. After some debugging, I found out that at least the correct ID of eloquent comes back. But somewhere this must have been discarded again.

For the moment, I would first adapt the documentation in the config that explain should not be used until we find the cause.

Does perhaps last_insert_id run after the explain query, instead of after the insert?

In that case, explain should never be used on inserts, and possibly also not on update/delete.

Should we just set it to select only?

I don't know if it is relevant but I originally thought my issue was from Laravel Telescope: https://github.com/laravel/telescope/pull/375; I thought it was a similar enough issue to mention.

To be clear, did you both enable explain AND add insert to the array? Because it should default to select only.

Yes I did
image

Okay I think we should probably only allow select then for now. Anyone up for a PR? Otherwise I'll take a look soonish

@barryvdh I was late for noticing but how is this PR? #903

And I checked the behavior of PDO.
From the output of this code, you can see that lastInsertId becomes 0 with EXPLAIN.
Therefore, if QueryCollector::addQuery is called before obtaining lastInsertId, will be 0.

<?php
try {
    $pdo = new PDO('mysql:host=localhost;dbname=root', 'root', 'root');
    $mail = time();
    $insertQuery = <<<SQL
INSERT INTO `users` (`name`, `email`, `password`)
VALUES
    ('name', $mail, 'pass');
SQL;
    $pdo->query($insertQuery);

    $beforeId = $pdo->lastInsertId('id');
    $explainQuery = "EXPLAIN {$insertQuery}";
    $pdo->query($explainQuery);
    $afterId = $pdo->lastInsertId('id');


    echo "before: {$beforeId}" . PHP_EOL;
    echo "after: {$afterId}";
} finally {
    $pdo = null;
}

Yeah so no way around that I suppose (or we'd have to do it BEFORE). But probably best to disable it for update/delete/insert.

Quick question: What does the (...) for MySQL 5.6.3+ means in the comment that was added on the config file? Is this behavior not the same with PostgreSQL or SQLite?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this issue is still present on the latest version of this library on supported Laravel versions, please let us know by replying to this issue so we can investigate further.
Thank you for your contribution! Apologies for any delayed response on our side.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hussein-elhussein picture hussein-elhussein  Â·  3Comments

lucasdcrk picture lucasdcrk  Â·  3Comments

Thijmen picture Thijmen  Â·  6Comments

thomthom picture thomthom  Â·  5Comments

coderdiaz picture coderdiaz  Â·  4Comments