Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#12587 closed (duplicate)

Related fields when using multiple databases and different managers

Reported by: aprilmay Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords: multiple related
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Let's take the case:

  • 2 databases in settings.py (default and otherdb)
  • 2 model managers, that will be used for managing models on different DBs:
class Manager1(models.Manager):
    def __init__(self):
        super(Manager1, self).__init__()
        self._db = 'default'

class Manager2(models.Manager):
    def __init__(self):
        super(Manager2, self).__init__()
        self._db = 'otherdb'


  • 2 models using a manager each, and a relation between the 2:
class MyModel1(models.Model):
    objects = Manager1()

class MyModel2(models.Model):
    objects = Manager2()
    rel_to_model1 = models.OneToOneField(MyModel1)

In that case the relations fail:

a_model2 = MyModel2.objects.all()[0]
a_model1 = a_model2.rel_to_model1

The last line raises an error because it uses the manager for model2 for searching model1.

Also,

a_model1 = MyModel1.objects.all()[0]
a_model2 = a_model1.mymodel2

The last line raises an error for the same reason than above.

These examples show (and the proposed patch is a fix for) the OneToOneField relation, but other relations might suffer from same kind of issue.

This is dummy code, hope it is good enough.

Attachments (1)

related_multiple_db.diff (1.4 KB) - added by aprilmay 5 years ago.

Download all attachments as: .zip

Change History (3)

Changed 5 years ago by aprilmay

comment:1 Changed 5 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #12540. Although I'm sure the patch you have provided solves the problem as you see it, the general problem will require a slightly more general solution. See the discussion referenced in #12540 for more details.

comment:2 Changed 4 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

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