I am using sqlalchemy with mysql database. When I am using following query on User object :
session.query(User).filter(User.name == 'admin').all()
I get all the results which have usename as 'Admin', 'admin', 'ADMIN' (basically all case-insensitive 'admin'). I would like to know how can I force a filter() to filter exact matches (without ignoring cases)?
Update : Actually I just came to know that mysql does not allow case-sensitive columns for varchar() datatype. So the simplest solution would be to force a column to be case-sensitive while declaring in mysql, as :
`name` VARCHAR(255) BINARY NULL UNIQUE,
But I would still love to know how can I force a filter to match the results exactly (not ignoring cases). Is there anyway using some inbuilt (or custom, if possible) sqlalchemy func?
select * from users where binary name = 'AdMin';
select * from users where binary name = 'admin';
from sqlalchemy import func
User.query.filter(User.name == func.binary('AdMin')).count()
User.query.filter(User.name == func.binary('admin')).count()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With