Count on valid queryset raise a "OperationalError"

I have some model that represent a contract and has some logic to get a related amount of money (monto_control_interno_registrado) that can be lest/equal to a expected amount (monto_control_interno). I intent to filter the records that has the related amount (monto_control_interno_registrado) equal to the expected amount (monto_control_interno), and works fine but when i make a custom filter in django admin page, the pagination process execute qs.count() and this raise a OperationalError: (1054, "Unknown column 'contratos.monto' in 'having clause'")

This is the related methods in the queryset…

class ContratoQueryset(models.QuerySet):
    def with_monto_control_interno(self):
        return self.annotate(
            monto_control_interno=F('monto') * F('cambio') *
            (F('porcentaje_interno') / 100)

    def with_monto_control_interno_registrado(self):
        return self.annotate(
                Sum('ingresos_control_interno__monto'), Decimal('0.00')

This is the query:


Note. The query get the correct result, the problem occurs when I add .count(), regardless of whether the query returns an empty queryset or with results, the OperationalError: (1054, "Unknown column 'contratos.monto' in 'having clause'") is throwed. I am using mysql :frowning: