问题出在Eloquent:未定义的函数:7错误:操作符不存在

huangapple go评论69阅读模式
英文:

Problem with Eloquent : Undefined function: 7 ERROR: operator does not exist

问题

我使用 postgresql 并且有 三个表

1: Habilitation(权限)

code              - 字符串(225)
code_menu  - 字符串(225) 
code_sub_menu  - 字符串(225) 
name             - 字符串(225)

2: menu(菜单)

code              - 字符串(225)
name             - 字符串(225)

3: submenu(子菜单)

code              - 字符串(225)
name             - 字符串(225)

我想要使用 Eloquent 显示带有它们的 habilitations(权限)、menu(菜单)和 sub-menu(子菜单),当我尝试:

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

我得到以下错误:

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))

我的模型:

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');
    }

}

我在使用 Laravel 的查询构建器时没有问题,但我想要使用 Eloquent。有人可以帮助我吗?或者给我一些建议吗?
提前感谢您。

英文:

I use postgresql and i have three table

1: Habilitation

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

2: menu

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

3: 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');
}
}

i haven't problem using query builder of laravel but i want to use Eloquent for it.
someone could help me ? or give me some hints ?
thank you in advance

答案1

得分: 4

我添加了public $keyType = 'string';来解决这个问题。
提示:https://www.tekmx.com/blog/using-non-standard-primary-key-with-eloquent-relations-laravel-5

英文:

i added public $keyType = ‘string’; to resolve this problem .
hints : https://www.tekmx.com/blog/using-non-standard-primary-key-with-eloquent-relations-laravel-5

答案2

得分: 0

这个错误与 PostgreSQL 相关。您需要检查适当的类型转换或列数据类型,因为 PostgreSQL 是严格的,您可以看到您已经将列强制转换为字符串,但在查询中实际上是整数。通常当您引用 INTVARCHAR 或反之时会发生这种情况。

这个查询:

select * from "menu" where "menu"."code" in (0, 0, 0, 0)

实际上应该是:

select * from "menu" where "menu"."code" in ('0', '0', '0', '0')

所以我建议将您的字符串数据类型更改为主键和外键的整数类型。

英文:

This error is related to PostgreSQL. You need to check for proper type casting or column data types as PostgreSQL is strict, and you can see you have type cast your columns to string but in the query it's actually integer. This normally happens when you are referencing INT to VARCHAR or vice versa.

This

select * from "menu" where "menu"."code" in (0, 0, 0, 0)

Should actually be

select * from "menu" where "menu"."code" in ('0', '0', '0', '0')

So I suggest changing your string data type to int for primary & foreign keys.

答案3

得分: 0

我在使用Postgres和MorphMany关系时遇到了相同的问题。
我通过在返回该关系的模型中将keyType设置为string来解决了这个问题,我没有直接将其设置为模型,因为我想要keyTypeinteger

public function merchantOrderable(): MorphMany
{
    $morph = $this->morphMany(OrderModel::class, 'merchant_orderable');
    $morph->getParent()->setKeyType('string');

    return $morph;
}
英文:

I had the same issue using Postgres and a MorphMany relationship.
I solved it by setting keyType to string in the model that returned the relationship, I did not set it directly to the model because I wanted keyType as integer.

public function merchantOrderable(): MorphMany
{
$morph = $this->morphMany(OrderModel::class, 'merchant_orderable');
$morph->getParent()->setKeyType('string');
return $morph;
}

答案4

得分: 0

请注意使用 PostgreSQL 中的 uuid 的开发者们。如果您在 Laravel 中使用 Trait,有很高的概率会遇到相同的问题。

class ShoppingSession extends Model
{
    use HasFactory, Uuid;

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

    protected $primaryKey = 'id';
    protected $fillable = [
        'user_id',
        'total',
    ];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function shoppingCart(): HasMany
    {
        return $this->hasMany(ShoppingCart::class);
    }
}
英文:

Attention developers who use uuid in PostgreSQL. If you are using Trait in Laravel, there is a high probability that you will encounter the same problem.

class ShoppingSession extends Model
{
use HasFactory, Uuid;
protected $casts = [
'id' => 'string',
];
protected $primaryKey = 'id';
protected $fillable = [
'user_id',
'total',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function shoppingCart(): HasMany
{
return $this->hasMany(ShoppingCart::class);
}
}

huangapple
  • 本文由 发表于 2020年1月3日 15:06:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定