wanijkesv's avatar

Best place to add static data during a migration?

Hi,

I was wondering - I know that migrations are used for altering database table structure, and seeders are used to seed tables with rows.

In the case of what happens when you need to add categorical data - data that must be added as a one-time thing, like "document types", or "tax values" or "names of cities", where the application needs this starting data to run, but it is not a user-created record... is it still a good idea to place this in the seeder?

I've never used a seeder in a work-related application, but I hear a seeder is to seed tables generally with fake data especially with factories. But if I am interested in not seeding random data, but specific records that will be present for the lifetime of the application, should I still use a seeder?

To accomplish this task currently - I always insert categorical records through an imported php script in php artisan tinker. In it, I call model::create(data) for every record that i need via an imported function.

If seeders are indeed the best place, how does one run just the single seeder created? If I make more seeders, how can I prevent the categorical data from being deleted and remade (hence, breaking the id's and their relationships)?

0 likes
5 replies
rodrigo.pedra's avatar

I used to have a Seeder for this kind of data, that I'd run only once when installing the project.

Then I realized doing this kind of static hydration is better suited on a migration, in my opinion. After all, it is a run-off, and the database is incomplete, business logic-wise, if these data is not properly inserted.

When these categories are changed (new ones added, updated, or deleted), just create a new migration.

When I know beforehand any initial data to these tables, I place them in the same migration that creates the related table.

wanijkesv's avatar

Hi,

I was wondering - I know that migrations are used for altering database table structure, and seeders are used to seed tables with rows.

In the case of what happens when you need to add categorical data - data that must be added as a one-time thing, like "document types", or "tax values" or "names of cities", where the application needs this starting data to run, but it is not a user-created record... is it still a good idea to place this in the seeder?

I've never used a seeder in a work-related application, but I hear a seeder is to seed tables generally with fake data especially with factories. But if I am interested in not seeding random data, but specific records that will be present for the lifetime of the application, should I still use a seeder?

To accomplish this task currently - I always insert categorical records through an imported php script in php artisan tinker. In it, I call model::create(data) for every record that i need via an imported function. https://tutuappx.com/ If seeders are indeed the best place, how does one run just the single seeder created? If I make more seeders, how can I prevent the categorical data from being deleted and remade (hence, breaking the id's and their relationships)?

I got this,..

rodrigo.pedra's avatar

@wanijkesv

Maybe my answer is not well written. My opinion is that this kind of data is best fit into migrations, not seeders.

Of course, this is my personal opinion. Take it with a grain of salt.

how can I prevent the categorical data from being deleted and remade

If you properly set your foreign keys, the database won't allow data to be deleted if they have related records. Just don't set any cascade rules to the foreign key related to this data. The default action is called NO ACTION, precisely to avoid this kind of situation.

On the application level, you can rely on Model Events to prevent unwanted deletion.

https://laravel.com/docs/10.x/eloquent#events

psrz's avatar

@wanijkesv

You can use one seeder to insert/update your categorical data.

But instead of using Model::create() you use Model::updateOrCreate();

https://laravel.com/docs/10.x/eloquent#upserts

Personally, for most cases I use migrations like @rodrigo.pedra just explained, but there some exceptions. Like roles and permissions from the Spatie library. I like to keep the whole setup in one place.

I can call php artisan db:seed --class=PermissionSeeder as many times as I need and nothing breaks and everything is updated.

1 like

Please or to participate in this conversation.