[Python] cast column to bytea type in postgresql for pgm_sym_decrypt in sqlalchemy

Discussão em 'Python' iniciado por Stack, Julho 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm very new to fastapi and python altogether. I'm using pgcrypto module to encrypt the personal info of the customers. My raw query is

    select pgp_sym_decrypt(cast(email as bytea), 'secret_key') as email
    from customers


    and the query works fine. How to produce something similar in SqlAlchemy? I've tried something like this

    from sqlalchemy import select, func, cast, LargeBinary
    from sqlalchemy.dialects.postgresql import BYTEA


    customer = select(func.pgp_sym_decrypt(cast(Customer.c.email, 'bytea'), 'secret_key'))



    Also tried

    customer = select(func.pgp_sym_decrypt(cast(Customer.c.email, BYTEA), 'secret_key'))


    Also tried

    customer = select(func.pgp_sym_decrypt(cast(Customer.c.email, LargeBinary), 'secret_key'))


    But no luck anywhere. So, how to solve this problem? Any help would be appreciated. Thank you so much in advance.

    Continue reading...

Compartilhe esta Página