Reputation: 5087
I have an application that I want to port over to Laravel.
It already has data so I would need to port and set the database data using Migration and Seeding.
In my app, I have Table foo
and Table bar
. A field in Table bar
is a foreign key to a field in Table foo
.
When I seed, the id's inserted would most likely be different from the old application.
The problem then is that the integrity of the foreign key fields would be compromised since the ids' would be different.
Is there any strategy to solve this issue? I don't mind changing the values of the foreign key fields as long as it points to the correct row.
Upvotes: 1
Views: 1826
Reputation: 891
Does not depend of the language or framework, Laravel, Rails... the logic stays same.
You have to do some smarts select
on bar
table to be able to link foo
to bar
.
So in Laravel context you open FooTableSeeder.php
and write something like this:
$bar1 = Bar::where( ---your condition---)->first();
$bar2 = Bar::where( ---your condition---)->first();
DB::table('foo')->truncate();
$foogees = array(
array( 'bar_id' => $bar1->id ...),
array( 'bar_id' => $bar1->id ...),
array( 'bar_id' => $bar2->id ...),
...
);
DB::table('foo')->insert($foogees);
Upvotes: 1
Reputation: 127
I had a similar situation. My solution was to manually input the foo
id's. It worked well for me.
Normally, I wouldn't recommend setting the primary keys for a table, but since you're porting it, I think this is one of those situations where it would be acceptable.
Upvotes: 0