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

前端 未结 12 1192
眼角桃花
眼角桃花 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 06:11

    You also get this error if you do not create and populate your tables in the right order. For example, according to your schema, Comments table needs user_id, project_id, task_id and data_type_id. This means that Users table, Projects table, Task table and Data_Type table must already have exited and have values in them before you can reference their ids or any other column.

    In Laravel this would mean calling your database seeders in the right order:

    class DatabaseSeeder extends Seeder
    {
        /**
         * Seed the application's database.
         *
         * @return void
         */
        public function run()
        {
            $this->call(UserSeeder::class);
            $this->call(ProjectSeeder::class);
            $this->call(TaskSeeder::class);
            $this->call(DataTypeSeeder::class);
            $this->call(CommentSeeder::class);
        }
    }
    

    This was how I solved a similar issue.

    0 讨论(0)
  • 2020-11-27 06:12

    First delete the constraint "fk_comments_projects1" and also its index. After that recreate it.

    0 讨论(0)
  • 2020-11-27 06:13

    In case someone is using Laravel and is getting this problem. I was getting this as well and the issue was in the order in which I was inserting the ids (i.e., the foreign keys) in the pivot table.

    To be concrete, find below an example for a many to many relationship:

    wordtokens <-> wordtoken_wordchunk <-> wordchunks

    // wordtoken_wordchunk table
    Schema::create('wordtoken_wordchunk', function(Blueprint $table) {
            $table->integer('wordtoken_id')->unsigned();
            $table->integer('wordchunk_id')->unsigned();
    
            $table->foreign('wordtoken_id')->references('id')->on('word_tokens')->onDelete('cascade');
            $table->foreign('wordchunk_id')->references('id')->on('wordchunks')->onDelete('cascade');
    
            $table->primary(['wordtoken_id', 'wordchunk_id']);
        });
    
    // wordchunks table
    Schema::create('wordchunks', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->string('text');
        });
    
    // wordtokens table
    Schema::create('word_tokens', function (Blueprint $table) {
            $table->increments('id');
            $table->string('text');
    });
    

    Now my models look like follows:

    class WordToken extends Model
    {
       public function wordchunks() {
          return $this->belongsToMany('App\Wordchunk');
       }
    }
    
    class Wordchunk extends Model
    {
    
        public function wordTokens() {
            return $this->belongsToMany('App\WordToken', 'wordtoken_wordchunk', 'wordchunk_id', 'wordtoken_id');
        }
    }
    

    I fixed the problem by exchanging the order of 'wordchunk_id' and 'wordtoken_id' in the Wordchunk model.

    For code completion, this is how I persist the models:

    private function persistChunks($chunks) {
        foreach ($chunks as $chunk) {
            $model = new Wordchunk();
            $model->text = implode(' ', array_map(function($token) {return $token->text;}, $chunk));
            $tokenIds = array_map(function($token) {return $token->id;}, $chunk);
            $model->save();
            $model->wordTokens()->attach($tokenIds);
        }
    }
    
    0 讨论(0)
  • 2020-11-27 06:13

    Maybe you have some rows in the table that you want to create de FK.

    Run the migration with foreign_key_checks OFF Insert only those records that have corresponding id field in contents table.

    0 讨论(0)
  • 2020-11-27 06:16

    It just simply means that the value for column project_id on table comments you are inserting doesn't exist on table projects. Bear in mind that the values of column project_id on table comments is dependent on the values of ID on table Projects.

    The value 50dc845a-83e4-4db3-8705-5432ae7aaee3 you are inserting for column project_id does not exist on table projects.

    0 讨论(0)
  • 2020-11-27 06:16

    I had the same error, the problem was that I was trying to add role_id foreign to the users table, but role_id did not have a default value, so the DB did not allow me to insert the column because I already had some users and it didn't know how to add the column to them. Since I was in development I just used migrate:fresh, but if I was on production, I would probably set a default value to the role_id and not make it not constrained until I had the corresponding role on the DB.

    0 讨论(0)
提交回复
热议问题