Laravel - 1066 不是唯一的关系表/别名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43527886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - 1066 Not unique table/alias on a relationship
提问by Hadrien
I'm trying to create a simple relationship between tables :
我正在尝试在表之间创建一个简单的关系:
- attribute_type -
id
name
- category -
id
name
description
So I created a pivot table to link them :
所以我创建了一个数据透视表来链接它们:
- attribute_type_category -
attribute_type_id
category_id
There is the model relationships :
有模型关系:
On AttributeType.php
在 AttributeType.php 上
public function category() {
return $this->belongsToMany('App\AttributeTypeCategory', 'attribute_type_category', 'attribute_type_id', 'category_id');
}
On AttributeTypeCategory.php
在 AttributeTypeCategory.php 上
public function category() {
return $this->belongsToMany('App\Category');
}
All seems to be fine, but I get the following error :
一切似乎都很好,但我收到以下错误:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'attribute_type_category' (SQL: select
attribute_type_category
.*,attribute_type_category
.attribute_type_id
aspivot_attribute_type_id
,attribute_type_category
.category_id
aspivot_category_id
fromattribute_type_category
inner joinattribute_type_category
onattribute_type_category
.id
=attribute_type_category
.category_id
whereattribute_type_category
.attribute_type_id
= 1)
SQLSTATE[42000]:语法错误或访问冲突:1066 不是唯一的表/别名:'attribute_type_category'(SQL:select
attribute_type_category
.*,attribute_type_category
.attribute_type_id
aspivot_attribute_type_id
,attribute_type_category
.category_id
aspivot_category_id
fromattribute_type_category
internal joinattribute_type_category
onattribute_type_category
.id
=attribute_type_category
.category_id
whereattribute_type_category
.attribute_type_id
= 1)
Do you have any idea ? Thank you !
你有什么主意吗 ?谢谢 !
回答by Ameer Salah Aldeen
when you want to create simple many to many relation between two tables
like attribute_type
and category
,
you should create three tables using migrations as you did
当你想创建简单的多对多关系就像两个表之间attribute_type
和category
,你应该使用迁移像你一样创建三个表
attribute_type - id name
category - id name description
attribute_type_category - attribute_type_id category_id
attribute_type - id 名称
类别 - id 名称描述
attribute_type_category - attribute_type_id category_id
then you will create two classes (attribute_type and category) no need to create third one for the relation.
然后您将创建两个类(attribute_type 和 category),无需为关系创建第三个类。
and in attribute_type you should define method for the category relation
并且在 attribute_type 中,您应该为类别关系定义方法
public function category() {
return $this->belongsToMany('App\Category');}
and in category class:
在类别类中:
public function attributeType() {
return $this->belongsToMany('App\AttributeType');}
and then you can access the categories of any attribute_type by using ->categories
and you can access the attributeTypes of any category by using ->attributeTypes
然后你可以使用访问任何attribute_type的类别,->categories
你可以使用访问任何类别的attributeTypes->attributeTypes
you should follow laravel official documentation to learn more about relations https://laravel.com/docs/5.4/eloquent-relationships
您应该按照 laravel 官方文档了解有关关系的更多信息 https://laravel.com/docs/5.4/eloquent-relationships