On 07/13/2010 02:43 PM, Andrew Bialecki wrote:
> Any thoughts on a workaround?
>
Since you're selecting individual columns instead of mapped classes, the
ORM doesn't provide much benefit over straight SQL expressions. Try this
(untested):
by_email = select([User.email.label("text"), User.email.label("email")])
by_email = by_email.where(User.email.like("%%%s%%" % search_term))
by_first_name = select([User.first_name.label("text"),
User.email.label("email")])
by_first_name = by_first_name.where(User.first_name.like("%%%s%%" %
search_term))
q = union_all(by_email, by_first_name)
for (test, email) in session.execute(q).fetchall():
...
-Conor
> On Tue, Jul 13, 2010 at 3:40 PM, Michael Bayer <[email protected]>
> wrote:
>
>> that's an intricate trick which currently doesn't work with the ORM. #1852
>> is added to see if there's a quick way to fix this (very likely there isn't
>> something quick).
>>
>> You'd have to trick it using column("email") or something like that.
>>
>>
>> On Jul 13, 2010, at 3:02 PM, Andrew Bialecki wrote:
>>
>>
>>> I tried to write the following code assuming a User class with
>>> first_name, last_name, and email:
>>>
>>> search_term = "Andrew"
>>>
>>> by_email = session.query(User.email.label("text"),
>>> User.email.label("email")).filter(User.email.like("%%%s%%" %
>>> search_term)
>>> by_first_name = session.query(User.first_name.label("text"),
>>> User.email.label("email")).filter(User.first_name.like("%%%s%%" %
>>> search_term)
>>> matches = by_email.union_all(by_first_name).all()
>>>
>>> with the hope that this would return rows like so:
>>>
>>> text email
>>> ----- -------
>>> [email protected] [email protected]
>>> Andrew [email protected]
>>>
>>> However, I get:
>>>
>>> text email
>>> ----- -------
>>> [email protected] [email protected]
>>> Andrew Andrew
>>>
>>> When I look at the SQL, the wrapper select for the UNION is wrong.
>>> Any idea how to get the behavior I want? Thanks!
>>>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.