Opened 6 years ago

Closed 6 years ago

#13805 closed (worksforme)

Unable to use .raw() in multi-db environment

Reported by: liangent Owned by: nobody
Component: Uncategorized Version: 1.2
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


I have a model M. M.objects.raw('...') works, but M.objects.using('dbname').raw('...') doesn't work.

Attachments (1)

manager.patch (1.0 KB) - added by limscoder 6 years ago.
Patch to add option 'using' argument to method signature

Download all attachments as: .zip

Change History (5)

comment:1 Changed 6 years ago by Russell Keith-Magee

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Resolution: worksforme
Status: newclosed

It works fine for me, and I've got a test suite to back up my assertion.

If you're having difficulties using a feature, your first stop should be to ask on django-users.

comment:2 Changed 6 years ago by limscoder

Resolution: worksforme
Status: closedreopened

I have the same issue with 1.2.

The code M.objects.using('dbname').raw('...') causes the error: 'QuerySet' object has no attribute 'raw'.

The 'raw' method needs some way to specify which database to use. Perhaps a signature such as: .raw(sql, params, database=default)

comment:3 in reply to:  2 Changed 6 years ago by limscoder

Has patch: set

I attached a patch that implements the following signature: .raw(sql, params, using=default).

Changed 6 years ago by limscoder

Attachment: manager.patch added

Patch to add option 'using' argument to method signature

comment:4 Changed 6 years ago by Russell Keith-Magee

Resolution: worksforme
Status: reopenedclosed

Such an method already exists:


raw() isn't a queryset method, it's on the manager only. This is because you can't arbitrarily stick raw() on the end of any query, because the query might be filtered or otherwise modified in a way that raw() won't interact with.

You can call using() on the end of a raw() because a raw() queryset has already locked out other filter()-type options.

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