I have a simple player entity:
__tablename__ = 'player'
_id = Column('id', SmallInteger, primary_key=True)
_nickName = Column('nick_name', String)
def __init__(self, nickName):
self._nickName = nickName
@property
def id(self):
return self._id
@property
def nickName(self):
return self._nickName.decode(encoding='UTF-8')
@nickName.setter
def nickName(self, nickName):
self._nickName = nickName
when i do:
players = session.query(Player).filter(Player.nickName=='foo')
and i print the players var i got this:
SELECT player.id AS player_id, player.nick_name AS player_nick_name
FROM player
WHERE false
Obviously, when I add .first() at the end of the session query, the result is None.
I have tried with filter_by() and get the same result.
Any help is welcome.
While using
@hybrid_propertywill fix this in the general case, you shouldn’t need to be decoding manually at all. Just set the column type toUnicodeinstead ofStringand, assuming your server plays nice, you should correctly get back aunicode.You also don’t need the
idproperty at all.So all you should need for this class is:
(Both the column names and
__init__arguments can be generated automatically.)If there’s some reason your database isn’t handling Unicode correctly, well, that’s a different problem that we’d love to help you fix. 🙂