英文:
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
是严格的,您可以看到您已经将列强制转换为字符串,但在查询中实际上是整数。通常当您引用 INT
到 VARCHAR
或反之时会发生这种情况。
这个查询:
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
来解决了这个问题,我没有直接将其设置为模型,因为我想要keyType
为integer
。
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论