What i need is to get a migration describing the current structure in db and not what i defined in my models file because my models are not aligned to the structure of db, so i would like to get the current status and then apply my modifies defined in my models and make them aligned. Is it possible? And how?
1 Answer
Django-admin has a command to create models from a database: inspectdb. You can find the documentation here.
The idea would be to:
- first generate the initial models using
inspectdb, - review the result and change field types as needed, if you know better,
- then run
makemigrationsto get the migrations for your current db and - then start modifying your models (to what you have currently in your app) and
- run
makemigrationsagain.
It's a fairly complex operation, the recommended approach is to have two databases and move the data from the first to the new one. Django will add lots of tables to your db and you probably don't want this on your legacy db. This is nicely described in this blog post
2 Comments
Giuseppe
That's a possible way, but inspectdb need a manual inspection because not 100% reliable, so it can't be used into an automated process
dirkgroten
You'll always to do manual work, unfortunately, to integrate a legacy DB in a django app. I don't think this can be reliably automated. And since it's a one-off thing, I don't see why that's a problem
my models are not aligned to the structure of db?