ponyorm / pony

Pony Object Relational Mapper
Apache License 2.0
3.65k stars 245 forks source link

Trying to query by str.casefold() but that generates a AttributeError: 'str' object has no attribute 'casefold' #653

Open LaundroMat opened 2 years ago

LaundroMat commented 2 years ago

Here's a simple example, using Python 3.10.4 and pony 0.7.16

from pony.orm import *
db = Database()
class Person(db.Entity):
    name = Required(str)

db.bind(provider='sqlite', filename=':memory:')
db.generate_mapping(create_tables=True)

p1 = Person(name='John')
Person.exists(lambda p: str(p.name).lower() == 'john')   # True
Person.exists(lambda p: str(p.name).casefold() == 'john')   # AttributeError

That last line raises:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 2, in exists
  File "C:\***\lib\site-packages\pony\utils\utils.py", line 75, in cut_traceback
    reraise(exc_type, exc, last_pony_tb)
  File "C:\***\lib\site-packages\pony\utils\utils.py", line 88, in reraise
    try: raise exc.with_traceback(tb)
  File "C:\***\lib\site-packages\pony\orm\sqltranslation.py", line 1489, in getattr
    throw(AttributeError, '%r object has no attribute %r: {EXPR}' % (type2str(monad.type), attrname))
  File "C:\***\lib\site-packages\pony\utils\utils.py", line 101, in throw
    raise exc  # Set "pony.options.CUT_TRACEBACK = False" to see full traceback

AttributeError: 'str' object has no attribute 'casefold': str(p.name).casefold

I would like to use .casefold() because I want to make sure that e.g. the German word for street, Straβe, is considered equivalent to its alternative form Strasse (.casefold() turns Straβe intro strasse, while .lower() does not).

Sajadrahimi commented 2 years ago

Though this hasn't implemented yet, but the LOWER function does the same thing in MySQL if that helps. Also using .upper().lower() instead of .casefold() works in many cases.