2016-01-21 64 views
1

我正在做我的第一個laravel項目。我在我的數據庫中創建了兩個表並試圖在每個表中播種數據。第一個表(項目)的數據按預期插入,但沒有數據已被插入第二個表(任務)。而當我運行php artisan db:seed command我收到以下錯誤。laravel在數據庫中播種時出錯

「的約束違例:1062關鍵 PRIMARY重複條目‘1’

可能是什麼原因,以及如何我可以解決這個

這裏是我的大遷徙類

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); 
    } 
} 
+0

您的遷移對於這兩個表格的外觀如何? – Bogdan

+0

擴展遷移課程添加在帖子中。 –

+0

您第一次運行腳本?檢查數據庫中的表格。 – Naumov

回答

2

id列兩個表設置爲自動增量,所以只是從你插入陣列中刪除。因爲表中可能還有其他條目,所以只要讓MySQL處理id一代即可。


如果你想確保表格只包含您想要種子的值,可以清空每個播種類的表:

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

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

這樣您確保沒有可能導致限制違規的較舊條目,因爲重複的id值。