Opened 14 years ago

Closed 14 years ago

#13741 closed (invalid)

GenericForeignKey not working across multiple databases

Reported by: bartek Owned by: nobody
Component: Contrib apps Version: 1.2
Severity: Keywords: contenttype, genericforeignkey
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Karen Tracey)

It seems that you can't use a GenericForeignKey across multiple databases. My model is as follows:

class Item(Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey()
    # Other model fields, irrelevant to this example.

From my tests, the following two scenarios happen:

Scenario 1: I use the content_object approach, which uses the actual GenericForeignKey field to handle the data.

# Accessory is from a secondary database. I use routers to select it from the second database.

>>> a = Accessory.objects.get(pk=3)
>>> a
<Accessory: 1019 - Blue>
>>> i = Item(content_object=a, title="Test item")
>>> i.save()
>>> item = Item.objects.get(pk=1)
>>> item.content_object
<ATotallyDifferentModel: some_other_value>
>>> item.object_id
3L # This is correct

So it looks like it doesn't pick the right table to work with. It picks another model from the primary database, not going for the second database.

Scenario 2: I don't use content_object and set the id/type manually.

>>> ctype = ContentType.objects.get_for_model(Accessory)
>>> fi = Item.objects.create(object_id=a.pk, content_type=ctype, title="Some real item.")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/bartek/.virtualenvs/internal/lib/python2.6/site-packages/django/db/models/manager.py", line 138, in create
    return self.get_query_set().create(**kwargs)
  File "/home/bartek/.virtualenvs/internal/lib/python2.6/site-packages/django/db/models/query.py", line 352, in create
    obj.save(force_insert=True, using=self.db)
TypeError: save() got an unexpected keyword argument 'using'

So at this point I am at a loss. I asked around and no one knows if this is a bug or if I'm doing something wrong, so I am assuming it's a bug.

Change History (3)

comment:1 by Karen Tracey, 14 years ago

Description: modified (diff)

Fixed description formatting. PLEASE use WikiFormatting and preview before submitting.

in reply to:  1 comment:2 by bartek, 14 years ago

Replying to kmtracey:

Fixed description formatting. PLEASE use WikiFormatting and preview before submitting.

Sorry about that. I didnt realize until after posting, and then I couldn't figure out how to edit the ticket :/

comment:3 by Russell Keith-Magee, 14 years ago

Resolution: invalid
Status: newclosed

Generic keys do work with multidb, and there are quite a few tests in the multiple_database regression test to validate this.

As for point 1 -- it's impossible to diagnose further without seeing your router. The example you provide is explicitly tested (in several variants) in the multiple_database regression test, but the exact behavior will depend on your router.

As for point 2 -- I can only presume you've got a custom save method that doesn't accept the using argument, or you've somehow got a Django 1.1 install on your path somewhere. using is definititely a valid argument to save (and, by extension, to any well defined override save method).

I'm going to mark this ticket invalid on the basis that the ticket doesn't contain enough detail to diagnose that this is a problem and not user error. Feel free to reopen if you can provide more diagnosis info -- sample code, including full model and router definitions would be a minimum.

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