Opened 6 years ago

Closed 6 years ago

Last modified 5 years ago

#13358 closed (fixed)

RelatedManager does not work with db_manager

Reported by: craig.kimerer@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2-beta
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

RelatedManager is always selecting from the db_for_read provided by the router even if db_manager() is used.

With the following models:

class ModelA(models.Model):
  foo = models.IntegerField(default=0)

class ModelB(models.Model):
  bar = models.ManyToManyField(ModelA)

Running the following in shell:

>>> mod_a = ModelA.objects.create(foo=1)
>>> mod_b = ModelB.objects.create()
>>> mod_b.bar.add(mod_a)
>>> mod_a.modelb_set
<django.db.models.fields.related.ManyRelatedManager object at 0x102b4da50
>>> mod_a.modelb_set.db
'default_slave'
>>> mod_a.modelb_set.db_manager('default').db
'default'
>>> mod_a.modelb_set.db_manager('default').all()[0]._state.db
'default_slave'

Change History (3)

comment:1 Changed 6 years ago by Russell Keith-Magee

milestone: 1.2
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

For the record, it also affects reverse FK relations.

comment:2 Changed 6 years ago by Russell Keith-Magee

Resolution: fixed
Status: newclosed

(In [12993]) Fixed #13358 -- Ensured that db_manager() can be used to override database routing on M2M, reverse FK and generic key queries. Thanks to Craig Kimerer for the report.

comment:3 Changed 5 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

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