Exemplo n.º 1
0
# other option
query.order_by(User.id)
query.distinct()
query.limit()
query.offset()


# emit query and get result #################################

# the select Query instance will not emit a query until the
# following method invoked
query.all()              # fetch all as list
query.first()            # fetch one or None
query.one()              # fetch one or NoResultFound or MultipleResultsFound
query.one_or_none()      # fetch one or None or MultipleResultsFound
query.scalar()           # invokes the one() method and return the first column
result = query[1:3]      # list with limit and offset


# batch operator #########################################
# batch update and delete will emit the query immediately,
# return the number of row that changed.
changed_number = (Query(User)
                  .filter_by(name='a')
                  .update({User.name: 'b'}, synchronize_session=False))

delete_number = Query(User).filter_by(ID=6).delete()

# sub-query ##############################################
# use sub-query, act as mapped class