Opened 14 years ago

Last modified 14 years ago

#13741 closed

GenericForeignKey not working across multiple databases — at Version 1

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 (1)

comment:1 by Karen Tracey, 14 years ago

Description: modified (diff)

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

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