如何在func.count上执行HAVING?

时间:2017-02-17 09:36:21

标签: python sqlalchemy

这是我的代码:

    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)无法调整类型' 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_id,payment_profile.status AS payment_profile_status,payment_profile.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,count(%(param_1)s)AS charge_count \ nFROM payment_profile LEFT OUTER JOIN charge_log ON charge_log.profile_id = payment_profile._id \ nWHERE payment_profile.date_expiry_utc> =%(date_expiry_utc_1)s AND payment_profile.status =%(status_1)s AND charge_log.date_created_utc> payment_profile.date_charged_utc GROUP BY payment_profile._id,payment_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 count(%(param_1)s) =%(param_2)s'] [参数:{' date_expiry_utc_1&#39 ;: datetime.datetime(2017,2,14,9,2,11,353027),' param_1' :,' status_1':'有效',' param_2':0}]

我将其归因于'param_1': <class 'sb_base.model.ChargeLog'>charge_count未将having转换为要在var pixelWidth = Convert.ToInt32(userControl.RenderSize.Width); var pixelHeight = Convert.ToInt32(userControl.RenderSize.Height); RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(pixelWidth, pixelHeight, 96, 96, PixelFormats.Default); renderTargetBitmap.Render(userControl); PngBitmapEncoder pngImage = new PngBitmapEncoder(); pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap)); SaveFileDialog dlg = new SaveFileDialog { DefaultExt = "png", Filter = "Png Files|*.png" }; if (dlg.ShowDialog() == true) { using (Stream fileStream = File.Create(dlg.FileName)) { pngImage.Save(fileStream); } } 条件中解析的标签。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您需要使用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)