Laravel SQLSTATE[42000]:语法错误或访问冲突

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17908445/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:11:52  来源:igfitidea点击:

Laravel SQLSTATE[42000]: Syntax error or access violation

phpmysqllaravelrelationship

提问by Side

I am new to laravel and im stuck with my relationshops what looks like the following

我是 laravel 的新手,我坚持使用我的关系商店,如下所示

Categories

类别

    id  name            slug
-----------------------------------------------------------------
    3   Location        location
    4   Outfits         outfits
    5   Other           other

sub_categories

子类别

  id    category_id     name                slug
-----------------------------------------------------------------------------
     12     3           Club                club
     13     3           Home / Hotel        home-hotel
     14     3           Outdoor             outdoor
     15     3           Studio              studio
     16     4           Bikini / Swimwear   bikini-swimwear
     17     4           Dress               dress
     19     4           Jeans               jeans
     35     5           Dancing             dancing

Category model

品类模型

<?php

class Category extends Eloquent {

    public $timestamps = false;

    public function subcategory()
    {
        return  $this->belongsToMany('subcategory', "sub_categories");
    }
} 

And i get the following error

我收到以下错误

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'sub_categories' (SQL: select `sub_categories`.*, `sub_categories`.`category_id` as `pivot_category_id`, `sub_categories`.`subcategory_id` as `pivot_subcategory_id` from `sub_categories` inner join `sub_categories` on `sub_categories`.`id` = `sub_categories`.`subcategory_id` where `sub_categories`.`category_id` = ?) (Bindings: array ( 0 => 1, )) 

Could please someone point out what I am doing wrong?

请有人指出我做错了什么吗?

回答by echo_Me

you should give different aliases names to same table

你应该给同一个表提供不同的别名

change this

改变这个

 inner join `sub_categories`

to

   inner join `sub_categories` as sc   
                                   ^^--//-this alias use it instead of sub_categories

in your query it will be

在您的查询中,它将是

    select `sub_categories`.*, sc.`category_id` as `pivot_category_id`, sc.`subcategory_id` as `pivot_subcategory_id` from `sub_categories` inner join `sub_categories` sc on sc.`id` = `sub_categories`.`subcategory_id` where `sub_categories`.`category_id` = ?) (Bindings: array ( 0 => 1, )

Example:

例子:

 select * from table1 as t1
 inner join table1 as t2
on t1.id = t2.id

Clarification of your error:Not unique table/alias

澄清你的错误:Not unique table/alias

you are joining same table without aliases to different between them.

您正在加入同一张表,而它们之间没有别名。

回答by Joachim Isaksson

The problem looks like that two tables are mapped to the same name sub_category at once.

问题看起来像两个表同时映射到同名 sub_category。

A many-to-many relationship requires 3 tables (category/subcategory/category_subcategory) which need distinct names. If two of the tables get mapped with the same name, you'll get the error you're showing.

多对多关系需要 3 个需要不同名称的表(category/subcategory/category_subcategory)。如果两个表被映射为相同的名称,则会出现您显示的错误。

Change one of the mappings, and you should be up and running.

更改映射之一,您应该可以正常运行了。