cviebrock / sequel-pro-laravel-export

A Sequel Pro / Sequel Ace bundle to generate Laravel migration files from existing tables.
MIT License
921 stars 55 forks source link

Foreign key issue due to order migrations #11

Closed Wiejeben closed 7 years ago

Wiejeben commented 7 years ago

When generating the migration files it seems to create them in alphabetical order, this creates an issue when for example my "addresses" table (Will be inserted first) has a foreign key to table "cities" (Inserts later)

The following error is throwing when running php artisan migrate on a clean database:

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `addresses` add constraint `addresses_city_id_foreign` foreign key (`city_id`) references `cities` (`id`) on delete RESTRICT  
   on update RESTRICT)
cviebrock commented 7 years ago

The order they get created in is whatever Sequel Pro sets the list of selected tables to, which is likely alphabetically.

The tool is mostly for dumping table structures ... I can't think of any way you could make it smart enough to re-order them to avoid your situation.

Sorry.

johnpaulmedina commented 7 years ago

Why dont you write all foreign keys to a separate file that runs after all the exports? and then you have a foreign_key migration?

cviebrock commented 7 years ago

That might work. Feel free to submit a PR! ;)