Newbie question here, I am experienced with Django. I create a model and associated migration and run the migration to create the tables. However, if I then refactor the model significantly (add a bunch of foreign keys, change field types) the Django migration manager is pretty good about figuring out what needs to be done to bring the database in line with the model. Sometimes the best thing to do is to remove all the interim migrations and allow Django to create the initial migration fresh, but in Phoenix I started with mix phx.gen.schema
. What’s the right way to “start over” for a particular schema?
There’s nothing comparable in elixir/ecto. The biggest reason for that being that ecto does not enforce the notion of a 1:1 mapping between schemas and db tables. There can be multiple schemas per table and neither schema holding all the columns on the table. The ecto schema in your codebase is in no way a source of truth to implicitly describe the (complete) db table. DB tables and schemas can evolve completely unrelated from each other.
This is a quite different mindset I’d imagine from what I read Django’s doing.
The other part is that generators in phoenix are just “generators”. They’re neither part of ecto (the db layer) nor actually meant to be used for more than giving you a starting boilerplate. So to start over you could run the generator again, but it’ll just generate from scratch.
I agree, this represents a different approach philosophically between the two frameworks (neither good nor bad, just different).
So what would be the right approach if I decide that my first mix phx.gen.schema
was a bust and I need to do it again? How do I tell Phoenix to throw out the old migrations and how do I know which files I need to delete before I try to run the schema gen again?
You can get help with…
$ mix help phx.gen.schema
Generates an Ecto schema and migration.
You just need to delete the schema, and the migration file.
OK that was the only permutation of
help
I did not try, so TIL. Thank you for responding!
For future reference, you can just run mix help
in your project and it will show you all tasks that are relevant to it (libraries included).