DB Seeding Error: Column not found: 1054 Unknown column 'client_id' in 'field list'

DB Seeding Error: Column not found: 1054 Unknown column 'client_id' in 'field list'

我的顶级 table 是 clients 而 table users 属于 clients。这是我在尝试为用户的 table.

播种时遇到的错误

Illuminate\Database\QueryException : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'client_id' in 'field list' (SQL: insert into users (client_id, name, email, client_uuid, uuid, updated_at, created_at) values (test, test, 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';
    }
}

它告诉你没有列 users.client_id. 在某些时候,你一定有一个额外的列名称 users.client_id 除了 users.client_uuid. 删除你的表,运行再次迁移,然后重试播种机。