Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
v4.3.0 (202X-XX-XX)
-------------------

* Fixed `"multi-database support in inheritance accessors. <https://github.com/jazzband/django-polymorphic/pull/550>`_
* Fixed `Caching in inheritance accessor functions <https://github.com/jazzband/django-polymorphic/pull/510>`_
* Fixed `Foreign key resolves to parent class when using abstract models <https://github.com/jazzband/django-polymorphic/issues/437>`_
* Fixed `Support Q expressions that contain subquery expressions <https://github.com/jazzband/django-polymorphic/pull/572>`_
Expand Down
2 changes: 1 addition & 1 deletion src/polymorphic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def accessor_function(self):
rel_obj = field.get_cached_value(self)
except KeyError:
objects = getattr(model, "_base_objects", model.objects)
rel_obj = objects.get(pk=self.pk)
rel_obj = objects.using(self._state.db or DEFAULT_DB_ALIAS).get(pk=self.pk)
field.set_cached_value(self, rel_obj)
return rel_obj

Expand Down
19 changes: 19 additions & 0 deletions src/polymorphic/tests/test_multidb.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
ModelY,
One2OneRelatingModel,
RelatingModel,
RelationA,
RelationB,
RelationBase,
)


Expand Down Expand Up @@ -118,3 +121,19 @@ def func():

# Ensure no queries are made using the default database.
self.assertNumQueries(0, func)

def test_deletion_cascade_on_non_default_db(self):
def run():
base_db1 = RelationA.objects.db_manager("secondary").create(field_a="Base DB1")
base_db2 = RelationB.objects.db_manager("secondary").create(
field_b="Base DB2", fk=base_db1
)

ContentType.objects.clear_cache()

RelationBase.objects.db_manager("secondary").filter(pk=base_db2.pk).delete()

self.assertEqual(RelationB.objects.db_manager("secondary").count(), 0)

# Ensure no queries are made using the default database.
self.assertNumQueries(0, run)
Loading