数据库种子错误:找不到列:1054“字段列表”中的未知列“ client_id”

时间:2018-12-07 21:17:43

标签: php laravel eloquent uuid laravel-seeding

我的顶级表是clients,表users属于clients。这是我尝试为用户表添加种子时遇到的错误。

  

Illuminate \ Database \ QueryException:SQLSTATE [42S22]:列不   找到:1054“字段列表”中的未知列“ client_id”(SQL:插入   变成usersclient_idnameemailclient_uuiduuid,   updated_atcreated_at)值(测试,测试,test @ test.com,   412f251d-324b-472e-80ab-b06c5c61e732,   aaa4eaa0-fa63-11e8-9402-ebda32c76206,2018-12-07 21:04:20,2018-12-07   21:04:20))

客户端架构

Schema::connection('mysql_migrations')->create('clients', function (Blueprint $table) {
    $table->uuid('uuid')->primary();
    $table->string('name', 200);
    $table->string('password', 200);
    $table->timestamps();
});

用户架构

Schema::connection('mysql_migrations')->create('users', function (Blueprint $table) {
    $table->uuid('uuid')->primary();
    $table->uuid('client_uuid');
    $table->string('name');
    $table->string('email');
    $table->timestamp('accessed_at')->nullable();
    $table->timestamps();
});

客户播种者

DB::table('clients')->insert([
    'uuid' => '412f251d-324b-472e-80ab-b06c5c61e732',
    'name' => 'Example client',
    'password' => Hash::make('test'),
]);

用户播种者

factory(App\User::class, 5)->create([
    'client_uuid' => '412f251d-324b-472e-80ab-b06c5c61e732',
    'name' => 'test',
    'email' => 'test@test.com',
]);

客户模型

class Client extends Authenticatable implements JWTSubject
{
    use \BinaryCabin\LaravelUUID\Traits\HasUUID;

    protected $fillable = ['name'];

    public function users()
    {
        return $this->hasMany(User::class, 'client_uuid', 'uuid');
    }

    public function getRouteKeyName()
    {
        return 'client_uuid';
    }

    public function getKeyName()
    {
        return 'uuid';
    }

}

用户模型

class User extends Model
{
    use \BinaryCabin\LaravelUUID\Traits\HasUUID;

    protected $fillable = [
        'name', 'email', 'client_uuid',
    ];

    protected $dates = ['accessed_at'];

    public function client()
    {
        return $this->belongsTo(Client::class, 'client_uuid', 'uuid');
    }

    public function getKeyName()
    {
        return 'uuid';
    }
}

1 个答案:

答案 0 :(得分:1)

它告诉您没有列users.client_id.,在某个时候,您必须在users.client_id旁边有一个额外的列名users.client_uuid.删除表,再次运行迁移,然后重试播种者。