I'm making a basic blog. The user can add a post, filling in title, body, date. When submitted, the primary id field will obviously auto increment.
I'm using Laravel 5 seeding to populate the database with dummy data. The issue is, when I need to reseed data, I delete the prior rows, DB::table('articles')->delete();
then reseed.
The problem is, the autoincremented id
continues from the last id
. So If I'm seeding 10 rows on initial migrate, I get 1 thru 10. No problem. If I reseed, I get id
11-21.
This is problematic because I'm accessing the articles by ID, http://localhost/article/1
(Route::get('article/{id}'...
) but there obviously is no longer an ID of 1. The first article ID is now 11.
To get around this, I created a second id
column called article_id
: $table->primary('article_id')->unsigned();
as primary key so Laravel would query that.
- This causes problems now because on article insert, I need some way to auto increment the ID.
- Having a second
id
column seems unnecessary and bad way to make a schema
I'm sure this is a common issue- I'm wondering how to get around this.