fnews's avatar
Level 1

Custom name id in laravel migration

This is my pk for my users table.

$table->uuid('usr_id')->unique()->primary();

Famous solution is have code in my User model.

protected $primaryKey = 'usr_id';

Unfortunately, I still have error.

"message": "SQLSTATE[42703]: Undefined column: 7 ERROR: column "id" does not exist\nLINE 1: ... as aggregate from "users" where "email" = $1 and "id" <> $2\n

PS. I need help. Crud is the most important function in my system. And this problem keeps popping up whenever I update my data. I even use query builder instead of eloquent. But it still not working.

0 likes
1 reply
LaryAI's avatar
Level 58

The error message suggests that the "id" column is still being referenced somewhere in the code. One solution is to override the default incrementing behavior of the Eloquent model by setting the $incrementing property to false and the $keyType property to 'string' in the User model:

class User extends Model
{
    public $incrementing = false;
    protected $keyType = 'string';
    protected $primaryKey = 'usr_id';
    // ...
}

Then, in the migration file, use the following code to create the "usr_id" column as the primary key:

$table->uuid('usr_id')->primary();

Make sure to drop the "id" column if it already exists in the users table.

Please or to participate in this conversation.