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

前端 未结 12 1298
眼角桃花
眼角桃花 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.

提交回复
热议问题