A
A
Andrew Grinev @Andrew2018-09-21 14:00:04
Python
Andrew Grinev @Andrew, 2018-09-21 14:00:04

How to execute a query in Flask-SQLAlcheny?

The following models are available:

task_results = db.Table('task_results',
                        db.Model.metadata,
                        db.Column('task_id', db.Integer, db.ForeignKey('tasks.id')),
                        db.Column('account_id', db.Integer, db.ForeignKey('account_details.id')))

class Task(db.Model):
    __tablename__ = 'tasks'

    id = db.Column(db.Integer, primary_key=True)  
    accounts = db.relationship('AccountDetail', secondary=brut_task_results)

class AccountDetail(db.Model):
    __tablename__ = 'account_details'

    id = db.Column(db.Integer, primary_key=True)

Tell me, how can I select tasks that occur in the task_results table < 100 times?
In SQL, the query looks something like this:
SELECT * FROM tasks WHERE (SELECT COUNT(*) FROM task_results WHERE tasks.id = task_results.task_id) < 100

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lstdmi, 2019-02-12
@lstdmi

For the sake of interest, look towards
docs.peewee-orm.com/en/latest/peewee/querying.html#counting-records Tweet.select().where(Tweet.id > 50).count()50
also https:/ /stackoverflow.com/questions/40725044/peewe...
Perhaps the answer is here sqlalchemy.org .having
q = session.query(User.id).\
            join(User.addresses).\
            group_by(User.id).\
            having(func.count(Address.id) > 2)

but something doesn’t work to make the request what you need))
how-to-count-rows-with-select-with-sqlalchemy
free sql functions course about count, sum sqlalchemy core ampus.datacamp.com courses introduction-to-relatio...
# Import func
from sqlalchemy import func

# Build a query to select the state and count of ages by state: stmt
stmt = select([census.columns.state, func.count(census.columns.age)])

# Group stmt by state
stmt = stmt.group_by(census.columns.state)

# Execute the statement and store all the records: results
results = connection.execute(stmt).fetchall()

# Print results
print(results)

# Print the keys/column names of the results returned
print(results[0].keys())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question