#34623 closed Cleanup/optimization (duplicate)
Swappable dependencies always depend on first migration of app
Reported by: | Shai Berger | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This is a spin-off from https://github.com/django/django/pull/16861 and #23528.
When a migration specifies a swappable dependency, it is given as "app_label.Model"
. But at run-time that gets translated into "app_label.__first__"
, and the model name is ignored. This works as long as the model is defined by the time the dependent migration is run, but if the model is not created in the first migration of its app, then the right order of execution is no longer guaranteed.
This problem, if it manifests, is not at the level of either of the apps, but only the project.
The ideal solution is that a swappable dependency will actually mean "migrate that other app until the model shows up". That seems incompatible with making a migration plan in advance.
Another solution may be to load the app's migrations and look for a suitable CreateModel
operation. That is not ideal, because models can be created by other operations (obviously the built-in RenameModel
, but even an AddField
with a M2M, not to mention 3rd-party operations).
A different approach would be just to check -- when a swappable dependency is in place, after running the initial migration of the dependency app, verify that the requested model exists, and if it doesn't, error out. That may later be extended, by allowing a specific migration to marked (explicitly, in its code) as providing a specific model, but that extension may well be a YAGNI.
Change History (3)
follow-up: 2 comment:1 by , 17 months ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
follow-up: 3 comment:2 by , 17 months ago
Replying to Mariusz Felisiak:
It's probably a duplicate of #23694.
Yes, it is, but in the closing comment Andrew said "I'd love to know a way round it" and I believe I have suggested one here.
comment:3 by , 17 months ago
Replying to Shai Berger:
Replying to Mariusz Felisiak:
It's probably a duplicate of #23694.
Yes, it is, but in the closing comment Andrew said "I'd love to know a way round it" and I believe I have suggested one here.
Can you add a comment to the #23694? We should keep a discussion in the single place.
It's probably a duplicate of #23694.