'Django migration: Revert or bypass "ValueError: Dependency on app with no migrations"
while attempting to separate the models in different apps, I bumped to an issue I would need support on:
I have 2 models:
class SeriesCompletion(models.Model):
series = models.ForeignKey(
'Series', blank=True, null=True, on_delete=models.SET_NULL, )
user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True,
null=True, on_delete=models.SET_NULL,)
and
class Series(models.Model):
...
users_who_completed = models.ManyToManyField(
settings.AUTH_USER_MODEL, through='app1.SeriesCompletion',)
which were both located in app1
the idea (that worked well in dev environment) was to do a dumpadata, move SeriesCompletion to app2 and do the migrations, than a loaddata to repopulate properly
however, when moving to prod environment, I ran the app1 migration with the Series model with a reference to app2:
class Series(models.Model):
...
users_who_completed = models.ManyToManyField(
settings.AUTH_USER_MODEL, through='app2.SeriesCompletion',)
It passed in prod, and when moving to do the makemigration for app2, it blocked because of a circular reference: ValueError: <function ManyToManyField.contribute_to_class.<locals>.resolve_through_model at 0x7f3e531f4400> contains a lazy reference to app2.seriescompletion, but app 'app2' isn't installed.
I've tried a lot of things since, but I'm always blocked, and I am not able to move forwards doing a migration for app1 or for app2 ... or to move backwards returning to the previous migration with app1
I always end-up during the pre-migration/pre-makemigrations checks with the error message:
ValueError: Dependency on app with no migrations
Any bright idea to help me get out of this mess?
Solution 1:[1]
OK, it took us quite a lot of time, but we were finally able to get out of this mess.
As a matter of facts, we had somewhere in some old migration a migrations.RunPython(populate_SeriesCompletion) operation which of course was refering to the SeriesCompletion model ... and which was at the origin of the mess
I happen to run into retrocompatibility issues from time to time due to similar migrations.RunPython operations when models evolve.
Since some time I'm trying to avoid having migrations.RunPython operations in my migrations. I initially thought useing them was a good practice to avoid having things "lost in translation" ... but it seems on the contrary to prove a bad practice.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Skratt |
