How do I express “WHERE 'value' like column” in SQLAlchemy?

让人想犯罪 __ 提交于 2020-05-16 11:24:26

问题


I'm trying to do a where clause in reverse order with SQLAlchemy ORM. So instead of Table.query.filter(Table.column.like(value)), I'd like to end up with...

select * from table where 'mail.google.com' like domain;

...to select this row:

| domain       |
| ------------ |
| %.google.com |

Ideally, I'd be able to do this:

Table.query.filter(BinaryExpression('mail.google.com', Table.domain, custom_op('like')).all()

But it return the AttributeError: 'str' object has no attribute 'self_group'.

How is this expressed in SQLAlchemy?


回答1:


This works for me.

from sqlalchemy.sql.expression import literal
Table.query.filter(literal('mail.google.com').like(Table.domain)


来源:https://stackoverflow.com/questions/46783346/how-do-i-express-where-value-like-column-in-sqlalchemy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!