Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to force a filter() to match exact cases (case sensitivity) in sqlalchemy?

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?

like image 826
exAres Avatar asked Dec 26 '22 12:12

exAres


1 Answers

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()
like image 95
thinker3 Avatar answered Dec 28 '22 08:12

thinker3