Ask Your Question
0

How can I find an instrumented attribute in SQLAlchemy based on its length?

asked 2022-06-02 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-02-17 08:00:00 +0000

devzero gravatar image

You can use the length() function in SQLAlchemy to find attributes based on their length. Here's an example:

from sqlalchemy import length

# Assume we have a User model with a 'name' string attribute
# that we want to find based on its length

users = session.query(User).filter(length(User.name) > 10).all()

# This will return a list of all the user objects whose name attribute
# is longer than 10 characters

You can also use func.length() instead of just length() if you want to be more explicit about calling a function.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-06-02 11:00:00 +0000

Seen: 8 times

Last updated: Feb 17 '22