Opened 15 years ago

Closed 9 years ago

Last modified 9 years ago

#12768 closed Uncategorized (fixed)

Raw query map problem with some primary keys

Reported by: Reflejo@… Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello,

I'm trying the new raw query feature and i'm facing this bug:

When my model has primary_key set to some ForeignKey and I try to invoke the raw() method, InvalidQuery('Raw query must include the primary key') is raised.

class ExampleModel(models.Model):
    some = models.ForeignKey(SomeOther, primary_key=True)

    class Meta:
        db_table = 'example_model'


ExampleModel.objects.raw("SELECT some_id FROM example_model")

The problem arise because get_attname_column gives (some_id, some_id) as name/column but then it is compared to _meta.pk.attname (some) here:

        if skip:
            if self.model._meta.pk.attname in skip:
                raise InvalidQuery('Raw query must include the primary key')

Change History (5)

comment:1 by Alex Gaynor, 15 years ago

Resolution: duplicate
Status: newclosed

This is a dupe of another ticket (that one is labeled as being about model inheritance, but it's really about anyone where attname != name).

comment:2 by sklass@…, 13 years ago

Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset

I appreciate this is a dupe - can you please point us to the ticket#?

Thanks

comment:3 by Matt Johnson, 9 years ago

Resolution: duplicate
Status: closednew

This is broken again in 1.8. Looks like 0b6f05ede648ce62a5c91c7c38a0a362711f0656 caused the regression.

Here's my attempt at a patch: https://github.com/django/django/pull/5036


comment:4 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: newclosed

In e063ac2:

Fixed #12768 -- Fixed QuerySet.raw() regression on FK with custom db_column.

comment:5 by Tim Graham <timograham@…>, 9 years ago

In 05c7129b:

[1.8.x] Fixed #12768 -- Fixed QuerySet.raw() regression on FK with custom db_column.

Backport of e063ac2fae007a2eecaeab5ce17064c31230ce29 from master

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