这是我下面的播种班

<?php

use Illuminate\Database\Seeder;

class RequestTableSeeder extends Seeder
{
public function run()
{
     $faker = Faker\Factory::create();

     for($i=1;$i<=5;$i++){
        DB::table('requests')->insert([
            "location_id"=>$faker->numberBetween(1,5),
            "level_id"=>$faker->numberBetween(0,1),
            "subject_id"=>$faker->numberBetween(0,1),
            "first_name"=>$faker->firstName,
            "last_name"=>$faker->lastName,
            "contact"=>$faker->unique()->phoneNumber,
            "email"=>$faker->unique()->email,
            "description"=>$faker->text(1000),
            ]);
     }
}
}


这是我的levelseeder类:

<?php

use Illuminate\Database\Seeder;

class SubjectTableSeeder extends Seeder

{

public function run()
{


$faker = Faker\Factory::create();

    for($i=1;$i<=5;$i++)
    {
        DB::table('subjects')->insert([
            "name"=>$faker->text(5),
            ]);
    }
}
}


当我尝试从命令播种时,我得到:

 [PDOException]
  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`tutor`.`requests`, CONSTRAINT `requests_level_id_foreign` FOREIGN KEY (`level_id`) REFERENCES `levels` (`id`) ON DELETE CASCADE)


我还检查了我的主题播种器类。但是我找不到错误。This are my seeder class

最佳答案

您试图插入level_id引用level表中不存在的行!

为了使其正常工作,您的level表需要至少包含5条记录,且id分别为1,2,3,4,5

如果这样做,可能是您的播种机顺序错误。确保LevelTableSeederRequestTableSeeder之前运行。

此外,看来subject_id接下来很可能会失败。当使用foreign keys来确保数据库完整性时,这是预期的行为。

关于mysql - 播种时违反完整性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36442095/

10-11 01:38