-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Database Seeding causes "duplicate key" error #4897
Comments
Are you using Eloquent or fluent queries for the insert? Please show us the
|
The full code is long, so I put it as a separate Gist. For completeness, I included the migration and the Model. The Seed code is using I would guess that if my Seed did not include the |
Try ordering the inserts by ID, that might be the problem. I don't think the sequence is automatically set to a proper value after inserts. Not specifying the IDs should work better, though. |
|
@Gufran I am specifying the IDs on insert (because they need to match an existing system). |
Yes, I saw the gist. Your best bet would be to refresh the migrations before seeding ( |
I've just had the same problem when seeding after switching from MySQL to Postgres. It's only an issue when specifying the primary key values which I needed to do to preserve table relations. When I was using MySQL the sequence was updated automatically but not with Postgres. As @kohenkatz says, manually updating the sequence solves the problem but I can't help but feel it should be handled automatically. |
This is still a problem.
|
Just had same problem in Laravel 5.1 and PostgreSQL 9.4. Solved it manually using a raw query:
|
Still same error in 5.6 with |
$pdo = DB::getPdo(); |
I am using Laravel 4.2, with a Postgres 9.3 database. I have a model
Category
created with the following migration:I also have a seed file that inserts 280 categories into the database. After I run the seed command, I am unable to insert more categories. Instead, I get an error like this:
Examining the database, I see that the insertions done by the seeder have not incremented the
categories_id_seq
.If I run
select setval('categories_id_seq', (select max(id) + 1 from categories));
, this fixes the problem.I know that I can add execution of this SQL query to my seed file, but I would have thought that fixing the series should be built into the seeding process.
Thoughts?
The text was updated successfully, but these errors were encountered: