youngminz
youngminz

Reputation: 1434

How to delete the Django model but not the database?

I'm running a production server with Django. I want to delete a model that is no longer needed. Migration makes it easy to apply them to the database. However, I do not want to delete the database that I have accumulated so far. Is there a way to think that the migration manager has been deleted without actually deleting the database?

Upvotes: 1

Views: 1036

Answers (2)

Kevin Christopher Henry
Kevin Christopher Henry

Reputation: 48902

There are two basic approaches: changing how you run migrate, or changing the migration itself.

If this is a one-off change to existing databases then you should simply run migrate with the --fake option.

If you want this to apply more broadly, though, the --fake approach is too brittle. You'd have to always remember to run this migration separately from the others at risk of losing data.

In that case it would be better to edit the migration itself. Specifically, you could convert the DeleteModel operation into a RunSQL operation that simply records the delete as having been done with the state_operations argument.

Which way to go probably depends on the answer to this question: if you were to create your database from scratch, would you want the unused table to still exist, or not? If so, edit the migration file. If not, run once per existing database with --fake.

Upvotes: 2

Enix
Enix

Reputation: 4569

What you need is running migrate admin command with --fake option.

--fake

Marks the migrations up to the target one (following the rules above) as applied, but without actually running the SQL to change your database schema.

This is intended for advanced users to manipulate the current migration state directly if they’re manually applying changes; be warned that using --fake runs the risk of putting the migration state table into a state where manual recovery will be needed to make migrations run correctly.

You can refer to Django document for more explanation.

Upvotes: 1

Related Questions