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

390
Vistas
Raw Query with SQL function in SQLAlchemy/encode/databases

I'm a complete beginner at Python and FastAPI. I'm using FastAPI and I have a table where the requirement is to encrypt the personal information of the user using pgcrypto module of PostgreSQL. The raw query would be something like this which can be executed into any database client and it executes without any error

insert into customers (email, gender) values (pgm_sym_encrypt('hello@gmail.com', 'some_secret_key'), 'male')

How to execute this query using SQLAlchemy core or encode/databases? I've tried this

from sqlalchemy import func
query = f"""
    insert into customers (email, gender) values 
    (:email, :gender)
    """
await database.execute(query=query, values={'email': func.pgm_sys_encrypt('hello@gmail.com', 'secret_key'), 'gender': 'male'})

It didn't work. I also tried

query = f"""
    insert into customers (email, gender) values 
    (pgm_sys_encrypt('hello@gmail.com', 'secret_key'), :gender)
    """
await database.execute(query=query, values={'gender': 'male'})

This didn't work either. I've no idea how to execute a function in the raw query. Please help. I've tried so much but I'm totally clueless on this one still now. Thank you in advance for your help.

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

0

As it's a raw query you should be able specify it as you would in raw SQL, so this should work:

from sqlalchemy.sql import text
query = """
    insert into customers (email, gender) values 
    (pgm_sys_encrypt(:email, :secret_key), :gender)
    """
await database.execute(query=text(query), values={'email': 'hello@gmail.com', 'secret_key': 's3cr37', 'gender': 'male'})
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