Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kevin73's avatar

SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: character varying = integer

Hi , I use postgresql and i have three table :

Habilitation

code              - string(225)
code_menu  - string(225) 
code_sub_menu  - string(225) 
name             - string(225)

menu

code              - string(225)
name             - string(225)

submenu

code              - string(225)
name             - string(225)

I want to show habilitations with their menu and sub-menu using Eloquent , when i try :

Habilitation::with(['menu','submenu'])->get();

i get this error :

Illuminate\Database\QueryException
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: character varying = integer LINE 1: select * from "menu" where "menu"."code" in (0, 0, 0, 0) ^ HINT: No operator matches the given name and argument types. You might need to add explicit type casts. (SQL: select * from "menu" where "menu"."code" in (0, 0, 0, 0)) 

My models :

class Menu extends Model
{
    protected $table = "menu";
    public $primaryKey = "code";
    public $timestamps = false;

    protected $casts = [
        'code' => 'string',
    ];

    public function submenu()
    {
        return $this->hasMany('App\Models\SubMenu','code');
    }

    public function habilitation()
    {
        return $this->hasMany('App\Models\Habilitation','code');
    }
}

class SubMenu extends Model
{
    protected $table = "submenu";
    public $primaryKey = "code";
    public $timestamps = false;

    protected $casts = [
        'code' => 'string',
    ];

    public function menu()
    {
        return $this->belongsTo('App\Models\SubMenu','code');
    }

    public function habilitation()
    {
        return $this->hasMany('App\Models\Habilitation','code');
    }

}

class Habilitation extends Model
{
    protected $table = "habilitation";
    public $primaryKey = "code";
    public $timestamps = false;

    protected $fillable = [
        'code', ''code_menu','code_sub_menu'
    ];

    protected $casts = [
        'code' => 'string',
        'code_menu' => 'string',
        'code_sub_menu' => 'string'
    ];

    public function menu()
    {
        return $this->belongsTo('App\Models\Menu','code_menu','code');
    }

    public function submenu()
    {
        return $this->belongsTo('App\Models\SubMenu');
    }

}

someone could help me ? thank you in advance

0 likes
4 replies
jove's avatar

I have not used what you are trying to do so I can't help with it, but I see this:

protected $fillable = [
        'code', ''code_menu','code_sub_menu'
    ];

You have unbalanced quote here'. Not sure if this could cause the issues you are experiencing.

kevin73's avatar
kevin73
OP
Best Answer
Level 1

I added it to resolve the problem : public $keyType = ‘string’;

2 likes
Kallou's avatar

the error is usually caused by making a varchar field primaryKey public $keyType = 'string'; in the model will solve the problem

Please or to participate in this conversation.