Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#12768 closed Uncategorized (duplicate)

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

comment:1 Changed 5 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

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