calling LENGTH() in SQLite?

10 views
Skip to first unread message

James Hartley

unread,
Jul 29, 2019, 11:53:19 PM7/29/19
to sqlal...@googlegroups.com
Perhaps I have been up too many hours, but my syntax foo is fizzling.  Given the following class, I want to compute the string length of "position" instead of storing it as another attribute which can get out of sync.  eg.

class Position(Base):
    __tablename__ = 'position'
    id = Column(INTEGER, primary_key=True)
    timestamp = Column(TIMESTAMP, nullable=False)
    position = Column(TEXT, unique=True, nullable=False)

So to get all positions of string length 2, the following is not working:
from sqlalchemy.sql.expression import func
# ...
for position, in session.query(Position.position).filter(func.length(Position.position == 2):
            print(position)

Any insight offered would certainly be appreciated.

Thanks!

Simon King

unread,
Jul 30, 2019, 4:16:07 AM7/30/19
to sqlal...@googlegroups.com
Your example is missing a bracket. I think I would expect this to work:

for position, in
session.query(Position.position).filter(func.length(Position.position)
== 2):
print(position)

If it still doesn't work, can you show the query that it's generating?

Simon
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
> ---
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAKeNXXtURxf-eXNaXn1woYE_q9i%3DggeopN%2B_%3D9hQb3FWfqqyLQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages