I’ve been thinking a lot about unmanaged models / managed models, because I deal a lot with both regularly (probably more than the average user I think). We deal with read-only access to 3rd party db’s as well as specific views and stored procedures on db’s we own/administrate.
I’m wondering if there may not be some benefit to an option to ‘not track changes’ to particular ( and/or all) unmanaged models.
It would add more flexibility to allow if it allowed people to exclude any/all unmanaged models from migrations. Because the models are unmanaged - the question needs to be asked “Is there a time where we don’t need to be tracking changes?” I think the answer is, “Yes.”
Drawing from the django docs:
You should think of migrations as a version control system for your database schema.
makemigrations
is responsible for packaging up your model changes into individual migration files - analogous to commits - andmigrate
is responsible for applying those to your database.
With regards to models relating to dbs/tables out of your control (think 3rd party applications) do we need those changes historically? To give what I think it is a fitting analogy, we don’t track commits to 3rd party libraries (for example venv
directories or node_modules
are generally git ignored), so I think we should be able to determine if certain models could be ‘ignored’ from migrations.
In the cases of unmanaged models specifically here are my thoughts:
I think the goal is to get django to currrently track everything with them (similar to managed models) so migrations truly work as a ‘version control’ (see the ticket to ensure this works).
Being able to keep certain unmanaged models from being migrated would make djangos behavior more in line with the documentation and give more validity to the docs idea that migrations are analogous to commits. Similar to excluding 3rd party libraries from vcs, we should be able to exclude models (which may be related to 3rd party dbs) from migrations.
An issue was brought up by @charettes
- What happens if we stop ‘not including migrations’ half way through?
I think we consider it in the same vein as the current option of managing/not-managing models. In these cases we specifically allow a migration for those models, should they be getting changed from one to the other.
We could potentially implement some kind of check in the autodetector.
Scenario 1 no-migrations → migrations:
In this scenario, we could ‘include a migration for that change’ (maybe ‘CreateModel’ type).
Scenario 2 migrations → no-migrations:
In this scenario, we could include a migration that is a ‘DeleteModel’ type, and then any further changes would not be tracked by migrations.
This is just opening up ideas for discussion. I’m interested to hear what others have to say/think about this.