Reputation: 9066
I am doing my first laravel project .I have created two tables in my database and trying to seed data in each tables.Data for the first table(projects ) was inserted as expected but no data has been inserted in the second table(tasks).And when i run php artisan db:seed command
i am getting the following error.
"Integrity constraint violation: 1062 Duplicate entry '1' for key PRIMARY
What might be the reason and how i can solve this?
Here is my extended migration class
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();
});
}
seeder class for projects table
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);
}
}
for tasks table:
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);
}
}
Upvotes: 1
Views: 2868
Reputation: 44526
The id
column for both tables is set to auto increment, so just remove it from the arrays you're inserting. Because there might be other entries in the table with the same id
, so just let MySQL handle the id
generation.
If you want to make sure the tables only contain the values you want seeded, you can empty the tables in each seeding class:
...
DB::table('projects')->truncate();
DB::table('projects')->insert($projects);
and
...
DB::table('tasks')->truncate();
DB::table('tasks')->insert($tasks);
That way you're ensuring there are no older entries that can cause constraint violations because of duplicate id
values.
Upvotes: 2