So you are describing two different things. Apply a SQL function to every *query* is at the query level, and you would use the before_compile hook to add this criteria:
However, if you want a particular column to use lower() on both sides, that's a different question, as it applies only to specific columns using specific datatypes (strings). The easiest way to get that is to define a custom type, such as:
from sqlalchemy import Column
from sqlalchemy import func
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import TypeDecorator
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class LowerCaseString(TypeDecorator):
impl = String
class comparator_factory(TypeDecorator.Comparator):
def __eq__(self, other):
return func.lower(self) == func.lower(other)
def __ne__(self, other):
return func.lower(self) != func.lower(other)
class A(Base):
__tablename__ = "a"
id = Column(Integer, primary_key=True)
x = Column(LowerCaseString)
print(A.x == 'hi')
print(A.x != 'hi')
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.