Integrity constraint violation: 1452 Cannot add or update a child row:

前端 未结 12 1250
眼角桃花
眼角桃花 2020-11-27 05:32

I am trying to insert values into my comments table and I am getting a error. Its saying that I can not add or update child row and I have no idea what that means.

m

12条回答
  •  天涯浪人
    2020-11-27 05:58

    that means that the value for column project_id on table comments you are inserting not only doesn't exist on table projects BUT also project_id probably doesn't have default value. E.g. in my case I set it as NULL.

    As for Laravel you can consider this expressions as a chunk of code of a migration php file, for example:

    class ForeinToPhotosFromUsers extends Migration
    

    { /** * Run the migrations. * * @return void */

    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
        $table->unsignedBigInteger('photo_id')->nullable();// ! ! ! THIS STRING ! ! !
        $table->foreign('photo_id')->references('id')->on('photos');
    });
    }
    

    }

    Obviously you had to create the Model class(in my case it was Photo) next to all these.

提交回复
热议问题