【问题标题】:Trying to use string as foreign key in laravel尝试在laravel中使用字符串作为外键
【发布时间】:2020-11-29 17:29:06
【问题描述】:

我只是一个初学者,尝试在 laravel 中使用字符串作为外键,但在获取数据时出现此错误:-

SQLSTATE[42S22]:未找到列:1054 未知列“读取”在“where 子句”中(SQL:选择 from 作为 sender_id,计数(from)作为来自 messages 的 messages_count,其中 to = d3c364bb-0982-46ba-869a-24dbb2c50aea 和 read = 0 分组 from)

在这里我从收到的用户联系人和一组未读消息计数中获取

public function get(){
        $contacts = DB::table('received')
    ->where('user_id', Auth::user()->uuid)
    ->get();
    
     $unreadIds = Message::select(\DB::raw('`from` as sender_id, count(`from`) as messages_count'))
            ->where('to', Auth::user()->uuid)
            ->where('read', false)
            ->groupBy('from')
            ->get();

       
        $contacts = $contacts->map(function($contact) use ($unreadIds) {
            $contactUnread = $unreadIds->where('sender_id', $contact->friends_id)->first();

            $contact->unread = $contactUnread ? $contactUnread->messages_count : 0;

            return $contact;
        });

    return response()->json($contacts);
        }

收到表:-

 public function up()
    {
        Schema::create('received', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->id();
            $table->string('user_id', 36);
            $table->unsignedBigInteger('friends_id');
            $table->string('list_no')->nullable();
            $table->string('name');
            $table->timestamps();
        });
    }

这是我从中获取未读消息计数的地方:-

public function up()
    {
        Schema::table('messages', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->boolean('read')->after('to', 36)->default(false);
        });
    }

    
    public function down()
    {
        Schema::table('messages', function (Blueprint $table) {
            $table->dropColumn('read');
        });
    }

这是消息表

public function up()
    {
        Schema::create('messages', function (Blueprint $table) {
           $table->engine = 'InnoDB';
            $table->id();
            $table->string('from', 36);
            $table->string('to', 36);
            $table->text('text');   
            $table->string('list_no')->nullable(); 
            $table->timestamps();
        });
    }

谁能帮我解决这个问题。

【问题讨论】:

  • “from”是SQL中的保留表达式,不推荐使用它作为字段名! + 我在表名中发现了一个错字:Schema::create('recieveds', function (Blueprint $table) {
  • @AdamP。我在输入这个问题时犯了那个错字,from 在我不使用字符串作为外键时工作得很好
  • 我建议阅读Eloquent Relationshops。不确定为什么要在迁移中定义引擎。什么版本的 MySQL 和 Laravel? MySQL 中的默认值现在是 InnoDB,因此不需要。
  • @mikeroq 我使用的是 laravel 8,我在this stack question看到了这个方法
  • @KakashiHatake 如果你注意到这个问题是从 2014 年开始的。Laravel 开发已经有很长一段时间了。

标签: php mysql laravel innodb


【解决方案1】:

仔细查看sql错误:形成的查询最初使用“from”而没有任何表名,因为您使用“from”作为名称,这是用于进行查询的SQL关键字。

在您的消息表迁移中尝试不同的列名称可能是“from_user”而不是“from”,并刷新迁移以反映数据库中的更改。

还要在代码中更改相关的列名。

注意 - 除了更改旧迁移之外,您还可以通过创建新迁移并添加 $table->renameColumn('from', 'from_user'); 来更改列名称

【讨论】:

  • 它没有说明要迁移,请您提供另一个答案
猜你喜欢
  • 2012-07-01
  • 2016-04-10
  • 2016-09-01
  • 2020-03-16
  • 2013-03-02
  • 1970-01-01
  • 2018-01-09
  • 2017-10-28
  • 1970-01-01
相关资源
最近更新 更多