Giuseppe
Giuseppe

Reputation: 393

Get current db migration status from db, not models in django

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?

Upvotes: 0

Views: 1097

Answers (1)

dirkgroten
dirkgroten

Reputation: 20682

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 makemigrations to get the migrations for your current db and
  • then start modifying your models (to what you have currently in your app) and
  • run makemigrations again.

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

Upvotes: 1

Related Questions