Home > other >  How to using sum function with encrypted column in SQLAlchemy?
How to using sum function with encrypted column in SQLAlchemy?

Time:12-19

I have used EncryptedType from sqlalchemy_utils to encrypt data of the specific column which will make when inserting data into the table or selecting data, the data of encrypted column will be encrypted.

This is the ORM structure of my database which have encrypted in value column.

from sqlalchemy_utils import EncryptedType
from sqlalchemy_utils.types.encrypted.encrypted_type import AesEngine

class Products(db.Model):
    __tablename__ = 'products'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(400))
    value = db.Column(EncryptedType(db.Integer, secret_key, AesEngine,'pkcs5')) 

And this is the result when select data in psql which you will unable to see the data of value column because it encrypted.

 id |  name   |                       value                        
---- --------- ----------------------------------------------------
  1 | Macbook | \x6977764a59556346536e6b674d7a6439312f714c70413d3d
  2 | IPhone  | \x6a6b51757a48554739666756566863324662323962413d3d
  3 | IPad    | \x416d54504b787873462f724d347144617034523639673d3d

But when i select data by using ORM, it will decrypt the data automatically.

And this is my code.

product_query = Products.query.order_by(Products.id.asc()).all()
for product in product_query:
    print(product.id, ' ', product.name, ' ', product.value)

Result

1   Macbook   222222
2   IPhone   40000
3   IPad   60000

Problem
When i try to select SUM with this code

db.session.query(func.sum(Products.value)).all()

it got the error like this.

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedFunction) function sum(bytea) does not exist
LINE 1: SELECT sum(products.value) AS sum_1 

Then as i understand the error, the problem is because the data that i try to SUM it still in byte or encrypted format, so are there any way that i can sum the value of encrypted column?

CodePudding user response:

If you have the column encrypted in the database, you cannot use a database function to add the values. The database doesn't even know the values. You will have to transfer the data to the client and calculate the sum there. Yes, this is slow and inefficient, but that is the price of security.

  • Related