Opened 9 months ago

Closed 9 months ago

Last modified 9 months ago

#22874 closed Bug (fixed)

Swappable dependency breaks if swappable model not in first migration of its app

Reported by: melinath Owned by: nobody
Component: Migrations Version: master
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Right now, it's impossible to declare a dependency to/from AUTH_USER_MODEL.

Here's the error I'm getting:

ValueError: Lookup failed for model referenced by field feedback.FeedbackItem.user: brambling.Person

This is bad on two fronts:

  1. From the "brambling" side - the side declaring/using an AUTH_USER_MODEL - I can't use the "feedback" module because it doesn't declare a dependency on my model - and django doesn't currently support reverse dependencies (unless they're undocumented.)
  1. From the "feedback" side - the side with migrations and a swappable FK - the app can't be used by anyone who actually has a swappable model, because there's no way to declare a correct migration dependency.

I feel like the correct fix here would be to have a 'swappable dependency'.

This is on stable/1.7.x

Change History (8)

comment:1 Changed 9 months ago by melinath

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from Need a way to declare a dependency for AUTH_USER_MODEL. to Swappable dependency not working

... wait, swappable dependencies are totally a thing. Okay. So why isn't this working?

comment:2 Changed 9 months ago by melinath

  • Summary changed from Swappable dependency not working to Swappable dependency breaks if swappable model not in first migration of its app

It seems like this wasn't working because swappable dependency automatically goes to the first migration in the swapped model's app - and if the swappable model isn't migrated into existence in the first migration, the swappable model dependency breaks.

I don't think this would be a serious problem in the future - brambling has some slightly older 1.7 migrations, from back when it would generate a million of them. I believe it's now consolidated down to one initial migration, so most people probably won't run into this.

Workaround was to add a dependency to the last migration in the swappable model's app. Would that work for the swappable model dependency behavior? (I could also imagine situations where a swappable model is switched out for a different one over the course of the app.)

comment:3 Changed 9 months ago by SmileyChris

  • Version changed from 1.7-beta-2 to master

comment:4 Changed 9 months ago by SmileyChris

Reverse dependencies are possible, just not documented - use run_before in the same way you use the dependencies attribute.

comment:5 Changed 9 months ago by SmileyChris

  • Severity changed from Normal to Release blocker
  • Triage Stage changed from Unreviewed to Accepted

This is a valid bug, not sure if it's considered release blocker - will bump up and let andrew decide.

comment:6 Changed 9 months ago by andrewgodwin

One of the caveats we had to do to be able to support swappable models was that they MUST be in the first migration of their respective app (as you've seen, otherwise dependency problems occur). There's literally no way to fix this, as it's logically impossible to construct a dependency graph otherwise with our model.

I'll add documentation highlighting this next to the existing warning about dependencies from unmigrated apps.

comment:7 Changed 9 months ago by Andrew Godwin <andrew@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 08221d1b5c1eec782c792818a2aa4a74ca7f1fcb:

Fixed #22874: Document that AUTH_USER_MODEL must be in first migration

comment:8 Changed 9 months ago by Andrew Godwin <andrew@…>

In 44b00af9bc3e6e8a561103aa5ca564995ca3c594:

[1.7.x] Fixed #22874: Document that AUTH_USER_MODEL must be in first migration

Note: See TracTickets for help on using tickets.
Back to Top