Reputation: 23
I have a model User in Laravel 6.5.1.
class User extends Authenticatable
{
use Notifiable;
public $table = 'usuario';
public $primaryKey = 'cif_usu';
public $incrementing = false;
public $timestamp = false;
//...
}
However, when I'm trying to select a user, I'm getting the following error:
SQLSTATE[42703]: Undefined column: 7 ERROR: column "id" does not exist LINE 1: select * from "usuario" where "id" = $1 limit 1 ^ (SQL: select * from "usuario" where "id" = 24 limit 1)
How can I rename the id column?
Edit:
I changed:
public $primaryKey = 'cif_usu';
to:
protected $primaryKey = 'cif_usu';
and the result is the same
Upvotes: 2
Views: 11553
Reputation: 1814
Put primarykey and table as protected.
protected $table = 'usuario';
protected $primaryKey = 'cif_usu';
public $incrementing = false; //only if your primary key is an assignable field,not auto incremented
You can use model as
DB::table('usuario')->where('cif_usu',$cif_usu)->first();
OR
DB::table('usuario')->find($cif_usu);
Upvotes: 0
Reputation: 287
Eloquent will also assume that each table has a primary key column named
id
. You may define a protected$primaryKey
property to override this convention:
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'key';
Read more about Laravel - Primary Keys
Upvotes: 4
Reputation: 3567
The visibility of the $primaryKey
attribute should be protected
. I think without traffic that change you aren't actually overriding the primary key in the base Model class.
If that's not the case it may be useful to see the code that triggers this query
Upvotes: 0