Search code examples
pythonpostgresqlsqlalchemy

Sqlalchemy array_length function


Does SQLALCHEMY has a function to be used in queries for array_length?

ie:

ssn.query(models.MedicationLog.dose_date,
                         func.array_length(models.MedicationLog.
                                    dose_taken_times).label('dose_taken_times'))

Solution

  • Fixed it with using max and array_length

    data = ssn.query(models.MedicationLog.dose_date,
                             func.sum(func.array_length(
                                 models.MedicationLog.dose_taken_times, ARRAY_DEPTH
                             )).label('dose_taken_times')).order_by(models.MedicationLog.dose_date).\
            group_by(models.MedicationLog.dose_date).all()