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);
}
}
您的遷移對於這兩個表格的外觀如何? – Bogdan
擴展遷移課程添加在帖子中。 –
您第一次運行腳本?檢查數據庫中的表格。 – Naumov