bastinald / laravel-automatic-migrations

Automatic Laravel model migrations.
42 stars 11 forks source link

Add migration sequence to accomodate ->constrained() migration #4

Closed vincentkedison closed 3 years ago

vincentkedison commented 3 years ago

Hello @bastinald,

Thanks a lot for this awesome package. I'm Vincent from Indonesia, I recently explored your laravel-livewire-ui package. However I found a problem whenever I create migration for a table that has a foreign to other tables. An error would occurred because the migration of related table run before the foreign table exist:

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `table_products` add constraint `table_products_category_id_foreign` foreign key (`category_id`) references `id` (`categories`))

I tried to make some modification by sorting the model files using defined $migration_sequence variable. Using this variable, we can defined the migration running sequence.

If you think this is a good solution, you can freely accept this PR.

Thanks.

bastinald commented 3 years ago

Hey, this is a good idea, but one thing is that migration files are run before the automatic migrations.

The sequence would not accomodate for this, right? Also, are you coupling all your DB tables with models? Or using a mixture of files and models?

bastinald commented 3 years ago

I'm gonna merge and tweak this, I think it's a good idea regardless.

bastinald commented 3 years ago

This has been released under v 2.4.0. Only thing was I changed it to migrationOrder, hope that is ok.

Added info to readme here: https://github.com/bastinald/laravel-automatic-migrations#migration-order

Thank you.