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

chechogrom's avatar

Error in with method in a query to a model

Hello everyone, I am getting this error after doing this query, I migrated from Laravel 5.6 to 5.7 and this query started to fail

query

$asesores = Asesor::with(['usuario' => function ($q) {
            $q->orderBy('NombreCompleto')
            ->select('NombreCompleto AS id','NombreCompleto AS text', 'Id');
        }])
        ->get()
        ->pluck('usuario')
        ->toJson();

error, doing tests I saw that it is in the method with::('usuario') that I get the error

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

Asesor model

  protected $table = 'SETCON.Asesor';
  protected $primaryKey = 'idAspNetUser';
  protected $keyType = 'string';

public function usuario()
    {
        return $this->hasOne('App\User', 'Id');
    }

User model

    protected $primaryKey = 'Id';
    protected $keyType = 'string';
    protected $table = 'dbo.AspNetUsers';

public function asesor()
    {
        return $this->belongsTo('App\modelos\Asesor', 'Id');
    }

I still don't understand why I have this error, thanks in advance

0 likes
5 replies
Sinnbeck's avatar

Are you sure that idAspNetUser Asecor is of the same type as Id on User?

This seems to indicate that one is an integer (and I assume the other is a string)

character varying = integer
1 like
Nakov's avatar

@chechogrom I think it is telling you exactly what is wrong. You cannot compare an integer with a varchar. PostgreSQL is strict and does not do any magic typecasting for you.

So the join that you want to do has to be between ID's from the same data type, either integer or varchar.

1 like
Snapey's avatar

You are selecting NombreCompleto as id and then also Id

Don't you think this could be confusing?

chechogrom's avatar

my native language is Spanish, and the error was quite confusing for me, this clarifies a little more

chechogrom's avatar

I didn't do this query. I am in charge of migrating a project from Laravel 5.6 to 5.7, in version 5.6 this does not fail, but when changing version, it began to fail, I am reviewing well

Please or to participate in this conversation.