1

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);
 }
}
asked Jan 21, 2016 at 19:49
3
  • How do your migrations looks like for the two tables? Commented Jan 21, 2016 at 19:50
  • extended migration class is added in the post. Commented Jan 21, 2016 at 19:51
  • You first time run the script? check table in database. Commented Jan 21, 2016 at 19:52

1 Answer 1

2

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.

answered Jan 21, 2016 at 19:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.