[英]Laravel db:seed attempts to seed wrong table
我在這樣的用戶和nextOfKin模型之間建立了聯系
user.php的
public function nextOfKin()
{
return $this->hasOne(NextOfKin::class, 'user_id');
}
NextOfKin.php
public function user()
{
return $this->belongsTo(User::class);
}
我已經使用遷移創建了users
表和next_of_kins
表,並希望使用數據庫種子將數據添加到表中。 當我運行UsersTableSeeder
public function run()
{
factory(App\User::class)->create()->each(function ($user) {
$user->nextOfKin()->save(factory(App\NextOfKin::class)->make());
});
}
它返回一個錯誤
Illuminate \\ Database \\ QueryException:SQLSTATE [42P01]:未定義表:7錯誤:關系“ next_of_kin”不存在第1行:插入“ next_of_kin”(“ user_id”,“ firstname”,“ lastname ... ^(SQL:插入值“ next_of_kin”(“ user_id”,“ firstname”,“ lastname”,“ relationship”,“ address”,“ state”,“ country”,“ updated_at”,“ created_at”)中的值(1,Willie,McCullough,家人,密克羅尼西亞密西根州密歇根州3334 B里奇·克里夫斯(2019-07-03 13:35:48,2019-07-03 13:35:48)返回“ id”)
為什么遷移嘗試插入到next_of_kin
而不是next_of_kins
表中? 用戶表播種成功。 如何解決?
在模型中,您可以指定要使用的表:
class NextOfKin extends Model
{
protected $table= 'next_of_kins';
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.