SQLSTATE [23000]:违反完整性约束:1048列“ parent_id”不能为空

时间:2019-03-24 19:32:02

标签: php sql laravel eloquent

我试图在两个表“ controlleur”和“ enfant”之间建立1:n关系

我遇到了错误:

  

SQLSTATE [23000]:违反完整性约束:1048列'parent_id'不能为空(SQL:插入enfantsid_enfantusernamepsw,{ {1}},sexeparent_idupdated_at)...

enfantController

created_at

餐桌婴儿

public function store () 
{
    request()->validate([           
        'username'  => ['required'],
        'psw'       => ['required'],
        'sexe'      => ['required'] ,
        'parent_id' => ['required|int'],
    ]);

    $enfant= new enfant();

    $enfant->id_enfant = request('id_enfant');
    $enfant->username = request('username');
    $enfant->psw = request('psw');
    $enfant->sexe = request('sexe'); 
    $enfant->parent_id = request('parent_id');

    $enfant->save();

    return redirect ('/themes');
}

儿童模型

public function up()
{
    Schema::create('enfants', function (Blueprint $table) {
        $table->bigIncrements('id_enfant');
        $table->bigInteger('parent_id')->unsigned();
        $table->foreign('parent_id')
            ->references('id')
            ->on('controlleurs')
            ->onDelete('cascade');
        $table->string('username');
        $table->string('psw');
        $table->string('sexe');
        $table->timestamps();
     });        
}
public function controlleur()
{
    return $this->belongsTo(App\controlleur);
}

0 个答案:

没有答案