簡體   English   中英

如何在func.count上執行HAVING?

[英]How do I perform a HAVING on a func.count?

這是我的代碼:

    charge_count = func.count(ChargeLog).label('charge_count')
    q = (session.query(PaymentProfile, charge_count)
         .outerjoin(ChargeLog, ChargeLog.profile_id == PaymentProfile._id)
         .filter(PaymentProfile.date_expiry_utc >= datetime.datetime.utcnow() - datetime.timedelta(days=(TOTAL_TIMES_TO_CHARGE - charging_attempt_0idx)))
         .filter(PaymentProfile.status == payments.ProfileStatus.ACTIVE)
         .filter(ChargeLog.date_created_utc > PaymentProfile.date_charged_utc)
         .group_by(PaymentProfile)
         .having(charge_count == charging_attempt_0idx))

但是,我收到sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'DeclarativeMeta'的錯誤sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'DeclarativeMeta'

特別:

sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)無法適應類型'DeclarativeMeta'[SQL:'SELECT payment_profile._id AS payment_profile__id,payment_profile.sb_org_id AS payment_profile_sb_org_id,payment_profile.plan_id AS payment_profile_plan_us,payment_profile.stats date_created_utc AS payment_profile_date_created_utc,payment_profile.date_updated_utc AS payment_profile_date_updated_utc,payment_profile.date_charged_utc AS payment_profile_date_charged_utc,payment_profile.date_expiry_utc AS payment_profile_date_expiry_utc,payment_profile.payment_method_id AS payment_profile_payment_method_id,計數(%(PARAM_1)S)AS charge_count \\ n從payment_profile LEFT OUTER JOIN charge_log ON charge_log.profile_id = Payment_profile._id \\ nWHEREpayment_profile.date_expiry_utc> =%(date_expiry_utc_1)s AND payment_profile.status =%(status_1)s AND charge_log.date_created_utc>付款文件.date_charged_utc GROUP BY payment_profile._id,pa yment_profile.sb_org_id,payment_profile.plan_id,payment_profile.status,payment_profile.date_created_utc,payment_profile.date_updated_utc,payment_profile.date_charged_utc,payment_profile.date_expiry_utc,payment_profile.payment_method_id \\ nHAVING計數(%)(%)參數:{'date_expiry_utc_1':datetime.datetime(2017,2,14,14,9,2,11,353027),'param_1':,'status_1':'active','param_2':0}]]

我把它歸功於'param_1': <class 'sb_base.model.ChargeLog'>通過該charge_count不翻譯成標簽在被解析having條件。

我怎樣才能解決這個問題?

您需要使用ChargeLog表中的一列,而不是表映射對象本身。 使用主鍵:

# assumption: ChargeLog.id is the primary key
charge_count = func.count(ChargeLog.id).label('charge_count')

這將在SELECT列和HAVING子句中插入相同的表達式; 您通常不能在HAVING使用標簽:

SELECT
  -- more columns
  count(charge_log.id) AS charge_count
-- FROM, OUTER JOIN, WHERE, GROUP_BY
HAVING count(charge_log.id)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM