在laravel中的数据库中播种时出错

时间:2016-01-21 19:49:31

标签: php mysql laravel

我正在做我的第一个laravel项目。我在我的数据库中创建了两个表,并尝试在每个表中播种数据。第一个表(项目)的数据按预期插入但第二个表中没有插入数据(任务)。当我运行php artisan db:seed command时,我收到以下错误。

  

"完整性约束违规:1062重复输入' 1'为了钥匙   PRIMARY

可能是什么原因以及如何解决这个问题?

这是我的扩展迁移类

public function up()
{ 
    // 
    Schema::create('projects', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name')->default('');            
        $table->string('slug')->default('');            
        $table->timestamps();
    });

    Schema::create('tasks', function(Blueprint $table) {            

       $table->increments('id');            
     //  $table->integer('project_id')->unsigned()->default(0);         
       $table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');            
       $table->string('name')->default('');         
       $table->string('slug')->default('');         
       $table->boolean('completed')->default(false);            
       $table->text('description')->default('');            
       $table->timestamps();    

    });
}

项目

的播种机课程
class projectSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
        $projects = array(
            ['id' => 1, 'name' => 'Project 1', 'slug' => 'project-1', 'created_at' => new DateTime, 'updated_at' => new DateTime],
            ['id' => 2, 'name' => 'Project 2', 'slug' => 'project-2', 'created_at' => new DateTime, 'updated_at' => new DateTime],
            ['id' => 3, 'name' => 'Project 3', 'slug' => 'project-3', 'created_at' => new DateTime, 'updated_at' => new DateTime]
        );

        // Uncomment the below to run the seeder
        DB::table('projects')->insert($projects);
    }
}

用于任务表:

    public function run()
    {
        //
           $tasks = array(
            ['id' => 1, 'name' => 'Task 1', 'slug' => 'task-1', 'project_id' => 1, 'completed' => false, 'description' => 'My first task', 'created_at' => new DateTime, 'updated_at' => new DateTime],
            ['id' => 2, 'name' => 'Task 2', 'slug' => 'task-2', 'project_id' => 1, 'completed' => false, 'description' => 'My first task', 'created_at' => new DateTime, 'updated_at' => new DateTime],
            ['id' => 3, 'name' => 'Task 3', 'slug' => 'task-3', 'project_id' => 1, 'completed' => false, 'description' => 'My first task', 'created_at' => new DateTime, 'updated_at' => new DateTime],
            ['id' => 4, 'name' => 'Task 4', 'slug' => 'task-4', 'project_id' => 1, 'completed' => true, 'description' => 'My second task', 'created_at' => new DateTime, 'updated_at' => new DateTime],
            ['id' => 5, 'name' => 'Task 5', 'slug' => 'task-5', 'project_id' => 1, 'completed' => true, 'description' => 'My third task', 'created_at' => new DateTime, 'updated_at' => new DateTime],
            ['id' => 6, 'name' => 'Task 6', 'slug' => 'task-6', 'project_id' => 2, 'completed' => true, 'description' => 'My fourth task', 'created_at' => new DateTime, 'updated_at' => new DateTime],
            ['id' => 7, 'name' => 'Task 7', 'slug' => 'task-7', 'project_id' => 2, 'completed' => false, 'description' => 'My fifth task', 'created_at' => new DateTime, 'updated_at' => new DateTime]
        );

        //// Uncomment the below to run the seeder
        DB::table('tasks')->insert($tasks);
    }
}

1 个答案:

答案 0 :(得分:2)

两个表的id列都设置为自动增量,因此只需将其从您要插入的阵列中删除即可。因为表中可能有其他条目具有相同的id,所以让MySQL处理id代。

如果要确保表中只包含要播种的值,可以清空每个种子类中的表:

...
DB::table('projects')->truncate();
DB::table('projects')->insert($projects);

...
DB::table('tasks')->truncate();
DB::table('tasks')->insert($tasks);

通过这种方式,您可以确保没有旧条目可能因重复id值而导致约束违规。