Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.1K
Vistas
How to check if query in SqlAlchemy returns empty result?

I'd like to perform a query like so:

    query = users.select().where(users.column.id == id)
    res = await database.execute(query)

I would then like to add some error handling by checking res. if not res does not seem to be correct. What is the proper way to do this?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can try taking first object from your query and checking if it is Null like this

result = users.select().where(users.column.id == id).first()
if not result:
    print('query is empty')

The other way editing your code would be

res = query.first()

And then checking if it is Null

over 4 years ago · Santiago Trujillo Denunciar

0

Depending on the complexity of your query, it might be cheaper to wrap it inside a SELECT EXISTS(...) than to SELECT ... LIMIT 1, as Antonio describes. PostgreSQL probably knows to treat those queries the same, but according to this answer, not all DBMS might do so.

The beauty of such a solution is that it exists as soon as it finds any result -- it does as little work as possible and is thus as cheap as possible. In SQLAlchemy that would be sa.select([sa.exists(query)])

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda