Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.1K
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda