Code

Opened 4 years ago

Closed 3 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:

Description

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 3 years ago.
Patch to add option 'using' argument to method signature

Download all attachments as: .zip

Change History (5)

comment:1 Changed 4 years ago by russellm

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

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 follow-up: Changed 3 years ago by limscoder

  • Resolution worksforme deleted
  • Status changed from closed to reopened

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 3 years ago by limscoder

  • Has patch set

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

Changed 3 years ago by limscoder

Patch to add option 'using' argument to method signature

comment:4 Changed 3 years ago by russellm

  • Resolution set to worksforme
  • Status changed from reopened to closed

Such an method already exists:

M.objects.db_manager('dbname').raw('...')

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.