Issue with `filter_by`?

19 views
Skip to first unread message

Amos

unread,
Jul 25, 2012, 1:17:57 AM7/25/12
to sqlal...@googlegroups.com

I've defined a column declaratively like so

my_column = Column(Unicode(30), index=True, unique=True)

If I pass in an integer instead of a string, it will actually return all records that start with the string representation of that integer

>>> [obj.code for obj in Session.query(Model).filter_by(my_column=123).all()]

[u'123ad', u'123lpb', u'123xd8', u'123za0']

I would expect no results as no column exactly matches the string representation of my number

Christoph Zwerschke

unread,
Jul 25, 2012, 4:31:28 AM7/25/12
to sqlal...@googlegroups.com
Am 25.07.2012 07:17, schrieb Amos:
> >>> [obj.code for obj in
> Session.query(Model).filter_by(my_column=123).all()]
>
> [u'123ad', u'123lpb', u'123xd8', u'123za0']
>
> I would expect no results as no column exactly matches the string
> representation of my number

Your result will depend on how your database engine compares strings to
integers. I can imagine that on some databases you will get no results,
on some you get an Error, because they refuse to make automatic type
conversions, and on some you will get what you got because they convert
the string '123ad' to the integer 123 (this is how e.g. the C function
atoi works). I haven't tested it, but I can imagine that Postgres will
throw an error and MySQL will give your result. That's one of the many
reasons I prefer Postgres.

-- Christoph


tonthon

unread,
Jul 25, 2012, 8:20:29 AM7/25/12
to sqlal...@googlegroups.com
what result do you get with :

>>> [obj.my_column for obj in
Session.query(Model).filter_by(my_column=123).all()]

?

Le 25/07/2012 07:17, Amos a �crit :
> --
> You received this message because you are subscribed to the Google
> Groups "sqlalchemy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sqlalchemy/-/mGMKZ0NB9_AJ.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to
> sqlalchemy+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.


Amos

unread,
Jul 25, 2012, 6:49:54 PM7/25/12
to sqlal...@googlegroups.com
Thanks for the quick response Christoph. I would also prefer an error over non-sensical results. Good thing we're switching to Postgres soon!
Reply all
Reply to author
Forward
0 new messages