Opened 6 years ago

Closed 6 weeks ago

Last modified 6 weeks 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: master
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 Changed 6 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

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 Changed 4 years ago by sklass@…

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

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

Thanks

comment:3 Changed 6 weeks ago by mdj2

  • Resolution duplicate deleted
  • Status changed from closed to new

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 Changed 6 weeks ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from new to closed

In e063ac2:

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

comment:5 Changed 6 weeks ago by Tim Graham <timograham@…>

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