【问题标题】:SQLSTATE[42S22]: Column not found: 1054 Unknown column material_tags.material_uuidSQLSTATE [42S22]:未找到列:1054 未知列 material_tags.material_uuid
【发布时间】:2019-01-13 01:23:13
【问题描述】:

我已经搜索过遇到此错误的人,但我仍然找不到解决方案。

我收到了错误:

"SQLSTATE[42S22]: 未找到列: 1054 '字段列表'中的未知列 'material_tags.material_uuid' (SQL: select tags.*, material_tags.material_uuid as pivot_material_uuid, @987654327 @.tag_uuid as pivot_tag_uuid from tags inner join material_tags on tags.uuid = material_tags.tag_uuid where material_tags.material_uuid in (00-a--d 91b4-ff3d7d9f961a) 和tags.deleted_at 为空)"

如果我必须查看材料 05a36470-d0a0-11e7-91b4-ff3d7d9f961a 它应该是这样的

当我尝试在控制器上运行此代码时:

    public function show(Request $request, $id)
{
    $material   = Material::with('tags')->where(
        'uuid',
        $id
    )->first();

我的材料模型有这个:

    public function tags()
{
    return $this->belongsToMany('App\Models\Tag', 'material_tags');

}

所以我有一个存储所有标签的标签表,以及一个存储所有材料的材料表。我有 Material_tags 表来查看哪些材料有标签。

迁移时我的 create_materials_table

    public function up()
{
    Schema::connection('materials')->create('materials', function (Blueprint $table) {
        $table->uuid('uuid')
            ->primary();
        $table->string('title');
        $table->integer('viewing_time')
            ->default(15)
            ->comment('In seconds');
        $table->text('description')
            ->nullable();
        $table->uuid('organization_id')
            ->nullable();

        $table->timestamps();
        $table->softDeletes();
    });
}

我的 create_tags_table 迁移

    public function up()
{
    Schema::connection('materials')->create('tags', function (Blueprint $table) {
        $table->uuid('uuid')
            ->primary();
        $table->string('name')
            ->unique();

        $table->timestamps();
        $table->softDeletes();
    });
}

还有我的 create_material_tags_table 迁移

public function up()
{
    Schema::connection('materials')->create('material_tags', function (Blueprint $table) {
        $table->uuid('uuid')
            ->primary();
        $table->uuid('material_id');
        $table->uuid('tag_id');

        $table->timestamps();

        $table->foreign('material_id')
            ->references('uuid')
            ->on('materials')
            ->onDelete('cascade');
        $table->foreign('tag_id')
            ->references('uuid')
            ->on('tags')
            ->onDelete('cascade');
    });
}

【问题讨论】:

    标签: php sql laravel-5 phpmyadmin


    【解决方案1】:

    您必须指定自定义外键:

    public function tags()
    {
        return $this->belongsToMany('App\Models\Tag', 'material_tags', 'material_id', 'tag_id');
    }
    

    【讨论】:

    • 非常感谢!这几天我一直在处理这个问题。谢谢!
    猜你喜欢
    • 2016-04-09
    • 2012-08-18
    • 1970-01-01
    • 2018-02-21
    • 2015-12-08
    • 2018-11-02
    • 2016-01-12
    相关资源
    最近更新 更多