简体   繁体   English

Laravel JOIN SQLSTATE [23000] 问题:违反完整性约束:字段列表中的 1052 列“album_id”不明确

[英]Laravel JOIN Problem with SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'album_id' in field list is ambiguous

I have referenced other questions like mine, but I cannot figure out why I am getting the error in my example.我已经参考了像我这样的其他问题,但我不明白为什么我的例子中出现错误。

I have 3 table.我有3张桌子。 nn_album , nn_song and pivot table nn_song_album_song nn_album , nn_song 和数据透视表 nn_song_album_song

My table ID names are nn_album.album_id , nn_song.song_id , pivot table has id as a primary key and same column name "song_id" and "album_id"我的表 ID 名称是 nn_album.album_id , nn_song.song_id ,数据透视表具有 id 作为主键和相同的列名“song_id”和“album_id”

I can add an album but when i want to add a new song to an album, I'm getting this error on save function...我可以添加专辑,但是当我想在专辑中添加一首新歌曲时,我在保存功能上遇到此错误...

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'album_id' in field list is ambiguous (SQL: select album_id from nn_album inner join nn_song_album_song on nn_album . album_id = nn_song_album_song . album_id where nn_song_album_song . song_id = 21) SQLSTATE [23000]:完整性约束违规:1052列'album_id'在字段列表暧昧(SQL:选择album_idnn_album内部联接nn_song_album_songnn_albumalbum_id = nn_song_album_songalbum_id其中nn_song_album_songsong_id = 21)

My Song Model我的歌模型

public function albums()
{
    return $this->belongsToMany('App\Albums', 'nn_song_album_song', 'song_id', 'album_id');
}

My Album Model我的专辑模型

public function songs()
{
    return $this->belongsToMany('App\SongDetail', 'nn_song_album_song', 'song_id', 'album_id');
}

My controller Save Function我的控制器保存功能

$albums = request('albums');

if ($song_id>0)
{
    $entry = SongDetail::where('song_id', $song_id)->firstOrfail();
    $entry->update($data);
    $entry->albums()->sync($albums);
}
else
{
    $entry = SongDetail::create($data);
    $entry->albums()->attach($albums);
}

My View on Admin side for choose an album input (i can choose multiple album to add)我在管理端查看选择专辑输入(我可以选择添加多个专辑)

<div class="form-group form-float mainalbum" style="margin-bottom: 45px;border-bottom: solid 1px;">
    <div class="form-line">


        <select class="form-control show-tick" name="albums[]" id="albums" data-live-search="true" >

            @foreach($albums as $album)
                @if($album->album_id != 1)<option value="{{ $album->album_id }}" {{ collect(old('albums', $album))->contains($album->album_id) ? 'selected': '' }}>
                    {{ $album->album_name }}</option>@endif
            @endforeach


        </select>
        <select class="albums2" name="albums2[]" id="albums2" multiple>
            @foreach($albums as $album)
                @if($album->album_id != 1)<option value="{{ $album->album_id }}" {{ collect(old('albums', $album))->contains($album->album_id) ? 'selected': '' }}>
                    {{ $album->album_name }}</option>@endif
            @endforeach
        </select>
    </div>

</div>

Do you have any idea what's going on here?你知道这里发生了什么吗? What is my fault?我的错是什么? If you share with me your thoughts, i appreciate you.如果你和我分享你的想法,我很感激你。

Thank you.谢谢你。

Well you have really made things far more complicated than you need to, by naming everything against Laravel conventions.好吧,通过根据 Laravel 约定命名所有内容,您确实使事情变得比您需要的复杂得多。 If this were set up as expected, you would have models named Album and Song , stored in database tables albums and songs , with a pivot table called album_song .如果这被设置为预期的,你将有一个名为车型AlbumSong ,存储在数据库表albumssongs ,用所谓的透视表album_song Things would be much easier then.那时事情会容易得多。

But these conventions can be worked around if one reads the documentation :但是,如果您阅读文档,则可以解决这些约定:

As mentioned previously, to determine the table name of the relationship's joining table, Eloquent will join the two related model names in alphabetical order.如前所述,为了确定关系连接表的表名,Eloquent 将按字母顺序连接两个相关模型名称。 However, you are free to override this convention.但是,您可以随意覆盖此约定。 You may do so by passing a second argument to the belongsToMany method.您可以通过向belongsToMany方法传递第二个参数来实现。

In addition to customizing the name of the joining table, you may also customize the column names of the keys on the table by passing additional arguments to the belongsToMany method.除了自定义连接表的名称之外,您还可以通过向belongsToMany方法传递附加参数来自belongsToMany表上键的列名。 The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to.第三个参数是您在其上定义关系的模型的外键名称,而第四个参数是您要加入的模型的外键名称。

So, fixing your arguments we get:所以,修正你的论点,我们得到:

class SongDetail extends Model
{
    public function albums()
    {
        return $this->belongsToMany('App\Albums', 'nn_song_album_song', 'song_id', 'album_id');
    }
}

class Albums extends Model
{
    public function songs()
    {
        return $this->belongsToMany('App\SongDetail', 'nn_song_album_song', 'album_id', 'song_id');
    }
}

Which should define the relationship properly.哪个应该正确定义关系。 Your controller again defies convention, as Laravel resource controllers typically define separate methods for storing and updating.你的控制器再次违反惯例,因为 Laravel 资源控制器通常定义单独的存储和更新方法。 But I can make one suggestion to clean up the code you've shown:但是我可以提出一项建议来清理您显示的代码:

$albums = request('albums');

try {
    $entry = SongDetail::findOrFail($song_id);
    $entry->update($data);
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
    $entry = SongDetail::create($data);
}
$entry->albums()->sync($albums);

And in your view, if you want to pass an array of values you need to specify multiple on the <select> element.在您看来,如果您想传递一组值,您需要在<select>元素上指定multiple

Well what the sql error message is pointing you to this that album_id exists in both tables but you are selecting album_id without specifying what one you want hence it is ambiguous那么什么 sql 错误消息指向你这个专辑_id 存在于两个表中,但你正在选择专辑_id 而不指定你想要什么,因此它是不明确的

You can likely resolve this by instead specify the exact column on the line您可以通过在行上指定确切的列来解决此问题

return $this->belongsToMany('App\Albums', 'nn_song_album_song', 'song_id', 'album_id');

by changing it to通过将其更改为

return $this->belongsToMany('App\Albums', 'nn_song_album_song', 'nn_song_album_song.song_id', 'nn_song_album_song.album_id');

I am not 100% on this solution as this is not the most familiar syntax to me but specifying the table for the output parameters should resolve the issue.我不是 100% 对此解决方案,因为这对我来说不是最熟悉的语法,但指定输出参数表应该可以解决问题。 You may need to apply tho to both models.您可能需要将 tho 应用于这两个模型。 The actual solution may be a variation of this but you will solve this by more specifically specifying table names for the fields in your queries using the dot syntax mentioned here and in other answers.实际的解决方案可能是此的一个变体,但您将通过使用此处和其他答案中提到的点语法更具体地为查询中的字段指定表名来解决此问题。

When you joining two tables it joins column names also.当您加入两个表时,它也会加入列名。 You have to describe from which table you want to take album_id column.您必须描述要从哪个表中获取专辑_id 列。 eg nn_album.album_id or nn_song_album_song.album_id例如nn_album.album_idnn_song_album_song.album_id

Your sql query has to look like this: select nn_album.album_id from nn_album inner join nn_song_album_song on nn_album.album_id = nn_song_album_song.album_id where nn_song_album_song.song_id = 21您的 sql 查询必须如下所示: select nn_album.album_id from nn_album inner join nn_song_album_song on nn_album.album_id = nn_song_album_song.album_id where nn_song_album_song.song_id = 21

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel 6 错误:SQLSTATE[23000]:违反完整性约束:1052 where 子句中的列“id_perusahaan”不明确 - Laravel 6 Error : SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id_perusahaan' in where clause is ambiguous SQLSTATE [23000]:违反完整性约束:1052与laravel结合使用 - SQLSTATE[23000]: Integrity constraint violation: 1052 using join with laravel PDOException:SQLSTATE [23000]:违反完整性约束:1052 IN / ALL / ANY子查询中的列“ category_id”不明确 - PDOException: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'category_id' in IN/ALL/ANY subquery is ambiguous SQLSTATE [23000]:完整性约束违规:1052 order order中的&#39;created_at&#39;列不明确Laravel 5.5 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous Laravel 5.5 Laravel Eloquent SQLSTATE[23000]:违反完整性约束:1052 列...在 where 子句中不明确 - Laravel Eloquent SQLSTATE[23000]: Integrity constraint violation: 1052 Column ... in where clause is ambiguous SQLSTATE [23000]:完整性约束违规:1052 列“created_at”在 order 子句中不明确 Laravel 8 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous Laravel 8 SQLSTATE [23000]:完整性约束违规:1052 where子句中的列&#39;NRP&#39;不明确 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'NRP' in where clause is ambiguous SQLSTATE [23000]:违反完整性约束:1052 where 子句中的列“值”不明确 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'value' in where clause is ambiguous SQLSTATE[23000]:完整性约束违规:1052 - SQLSTATE[23000]: Integrity constraint violation: 1052 完整性约束违规:1052 where子句中的列&#39;id&#39;不明确 - Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM