#27311: Support unpickled models (e.g. read from cache) in migrations
-------------------------------------+-------------------------------------
     Reporter:  Odero                |                    Owner:  nobody
         Type:  New feature          |                   Status:  closed
    Component:  Migrations           |                  Version:  master
     Severity:  Normal               |               Resolution:  wontfix
     Keywords:  migrations,          |             Triage Stage:
  foreignkey, cache                  |  Someday/Maybe
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Markus Holtermann):

 * status:  new => closed
 * type:  Bug => New feature
 * version:  1.10 => master
 * keywords:  migrations, foreignkey => migrations, foreignkey, cache
 * resolution:   => wontfix
 * stage:  Unreviewed => Someday/Maybe


Old description:

> Trying to create the migration below which is failing. It seems like the
> same issue discussed and resolved in #24282.
>
> {{{
> def generate_invoices(apps, schema_editor):
>     A = apps.get_model('app1', 'A')
>     B = apps.get_model('app1', 'B')
>     User = apps.get_model('accounts', 'User')
>
>     a = A.objects.last()
>
>     b = B(
>         client=a.user,
>         status=5
>     )
>     b.save()
> }}}
>
> Also tried
>

> {{{
>     b = B(
>         client=User.objects.get(pk=a.user.pk),
>         status=5
>     )
> }}}
>
> In both cases I get the error:
> {{{
> Cannot assign "<User: Some User>": "B.client" must be a "User" instance.
> }}}

New description:

 Trying to create the migration below which is failing. It seems like the
 same issue discussed and resolved in #24282.

 {{{
 def generate_invoices(apps, schema_editor):
     A = apps.get_model('app1', 'A')
     B = apps.get_model('app1', 'B')
     User = apps.get_model('accounts', 'User')

     a = A.objects.last()

     b = B(
         client=a.user,
         status=5
     )
     b.save()
 }}}

 Also tried


 {{{
     b = B(
         client=User.objects.get(pk=a.user.pk),
         status=5
     )
 }}}

 In both cases I get the error:
 {{{
 Cannot assign "<User: Some User>": "B.client" must be a "User" instance.
 }}}

 Investigating the issue shows that it's due to the models being read from
 cache.

--

Comment:

 This is not really feasible as the unpickling of models from cache uses
 the global app registry
 
(https://github.com/django/django/blob/d5c0eb7d686ea89d6dbf787abc5bf18302c4e428/django/db/models/base.py#L1770).
 We would need to look up the models from the app registry at this state of
 the migration process. I have no idea how we could achieve that w/o
 passing `apps` around all the time.

 I also think it's not a good idea to cache objects during the migration
 process. You can easily end up with objects from previous model states
 that are completely invalid at a later stage because too many things
 changed in between that wasn't taken care of for cached model instances.

 I'm closing this as won't fix for the time being. If this is possible at
 some point we could re-evaluate the feature, but for now I doubt it's
 going to work out.

--
Ticket URL: <https://code.djangoproject.com/ticket/27311#comment:8>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.7eb8bee0650a096ee338b70826802619%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to