dtf1111 2018-08-06 06:21
浏览 71
已采纳

SQLSTATE [42S22]:找不到列:1054未知列material_tags.material_uuid

I have searched for people who encountered this error but I still can't find the solution.

I have been getting the error:

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'material_tags.material_uuid' in 'field list' (SQL: select tags.*, material_tags.material_uuid as pivot_material_uuid, material_tags.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 (05a36470-d0a0-11e7-91b4-ff3d7d9f961a) and tags.deleted_at is null)"

in which if I have to view Material 05a36470-d0a0-11e7-91b4-ff3d7d9f961a it should look like this enter image description here

When I try to run this code located on the controller:

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

where my Material Model has this:

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

}

So I have a Tags table where all the tags are stored, and a Materials table that has all the materials stored. and I have the Material_tags table to see what Materials have tags.

my create_materials_table at migration

    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();
    });
}

my create_tags_table migration

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

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

and my create_material_tags_table migration

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');
    });
}
  • 写回答

1条回答 默认 最新

  • doubao6936 2018-08-06 13:48
    关注

    You have to specify the custom foreign keys:

    public function tags()
    {
        return $this->belongsToMany('App\Models\Tag', 'material_tags', 'material_id', 'tag_id');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么