catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.83k stars 1.15k forks source link

Last inserted ID Return 0 When Primary Keys UUID Format #817

Closed takpunya closed 3 years ago

takpunya commented 5 years ago

Describe the bug

I have primary key with UUID and datatype is binary(16).

Tryin to get last primary key after insert() but always returning to 0 $database->id();

when perform last query, it also showin 0 to primary keys value. $database->last();

Is it a bug or there something that i missed?

I just see id() is using using lastinsertedid:

return $this->pdo->lastInsertId();

http://php.net/manual/en/pdo.lastinsertid.php

So it temporary solved by changed primary key to auto increment, but not working if my primay key only uuid.


Information


Detail Code

$database->insert("users", [
    "id" => $database::raw("UUID()"),
        "username" => user,
        "password" => 1234
]);

$last_uuid = $database->id();

Expected output $last_uuid return to row id but currently always return to 0

deawx commented 5 years ago

MySQL: "With no argument, LAST_INSERT_ID() returns a BIGINT UNSIGNED (64-bit) value representing the first automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement."

MariaDB: "LAST_INSERT_ID() (no arguments) returns the first automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement."

This is clearly not what lastInsertId's own documentation states. Hopefully this will save someone from debugging the cause of id mismatch.

REF: http://php.net/manual/en/pdo.lastinsertid.php