dongmu2026 2015-10-21 21:56 采纳率: 100%
浏览 34
已采纳

外键上的Laravel 5 DB播种器“未找到列”错误

I’ve followed the Laravel docs, as well as a few tutorials, but I must be missing something as I’m getting a “Column not found” error on a foreign key. I’m hoping someone may be able to point it out.

I think the problem is that I don’t know how to “pass” the id for each User I create when trying to create a UserDetail record in the following:

factory(User::class, 3)
            ->create()
            ->each(function($u) {
                    $u->userdetail()->save(factory(UserDetail::class)->create());
});

The actual error is:

[Illuminate\Database\QueryException]                                                                                                 
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause'
(SQL: update `userdetails` set `user_id` = 1 where `id` = 0)   

My users table has an auto-incrementing id field which is a foreign key to the user_id field in the userdetails table.

Here are the Models:

/*
 * User Model
 */
class User extends Model {
    // …
    public function userdetail() {
        # User has a User Detail
        return $this->hasOne('Models\Users\UserDetail');
    }
    // …
}

/*
 * UserDetail Model
 */
class UserDetail extends Model {
    // …
    public function user() {
        # UserDetail belongs to User
        return $this->belongsTo('Models\Users\User');
    }
    // …
}

Here are the Seeders:

/*
 * UserTableSeeder
 */
class UserTableSeeder extends Seeder {
    public function run() {
        User::truncate();
        UserDetail::truncate();

        factory(User::class, 3)
            ->create()
            ->each(function($u) {
                $u->userdetail()->save(factory(UserDetail::class)->create());
            });
    }
}

/*
 * DatabaseSeeder
 */
class DatabaseSeeder extends Seeder {
    DB::statement('SET FOREIGN_KEY_CHECKS = 0'); // disable foreign key constraints
    Model::unguard();

    $this->call('UserTableSeeder');

    Model::reguard();
    DB::statement('SET FOREIGN_KEY_CHECKS = 1'); // enable foreign key constraints
}

And, finally, the Model Factories:

$factory->define(User::class, function (Faker\Generator $faker) {
    return [
        'uname'     => $faker->userName,
        'pass'      => bcrypt(str_random(8)),
        'email'     => $faker->email,
    ];
});

$factory->define(UserDetail::class, function (Faker\Generator $faker) {
    return [
        'street'    => $faker->streetName,
        'city'      => $faker->city,
        'state'     => $faker->stateAbbr,
        'zip'       => $faker->postcode,
        'phone'     => $faker->phoneNumber,
    ];
});

Thanks for any guidance!

  • 写回答

1条回答 默认 最新

  • dpo69086 2015-10-21 22:20
    关注

    The problem is when you're assigning the relationship. You have to use the make() method instead of the create() as the create() method will cause your model to be stored in the database while the make() method only creates the instance then the save() method will be the one to assign the required FK and store it in the database.

    factory(User::class, 3)
                ->create()
                ->each(function($u) {
                        $u->userdetail()->save(factory(UserDetail::class)->make());
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c