2015-09-14 84 views
1

顯然,我無法比較peewee SQL中的日期。peewee sql比較datetime Python

START_DATE = datetime.datetime(2015, 7, 20, 0, 0, 0)  
customer_records = Customers.select().\ 
        join(Current_Insurers, on=(Customers.current_insurer == Current_Insurers.id)).\ 
        switch(Current_Insurers).\ 
        join(Insurers, on=(Current_Insurers.insurer == Insurers.id)).\ 
        where(Customers.pol_type == "PC" & \ 
          Current_Insurers.effective_date == START_DATE) 

CustomersCurrent_InsurersInsurers三個class。結果始終爲0條記錄。但是,如果我刪除從SQL的datetime條件和比較如下

customer_records = Customers.select().\ 
         join(Current_Insurers, on=(Customers.current_insurer == Current_Insurers.id)).\ 
         switch(Current_Insurers).\ 
         join(Insurers, on=(Current_Insurers.insurer == Insurers.id)).\ 
         where(Customers.pol_type == "PC" 
for r in customer_records: 
    if(r.current_insurer.effective_date == START_DATE): 
     print(r.policy_id) 

令人驚訝的,我們現在可以比較和打印出來的客戶。

如何在peeweesql中添加datetime條件需要做些什麼?

非常感謝,

回答

1

顯然,我不能比較的peewee SQL的日期。

這完全不正確。你真的認爲圖書館會破?

問題是Python運算符優先級。您需要用括號包裝相等表達式。所以,你的where子句應該是這樣的,而不是:

where((Customers.pol_type == "PC") & \ 
     (Current_Insurers.effective_date == START_DATE)) 

此外,它通常只需要調用switch()當你有多個連接到一個單一的模式。

放在一起,您的查詢應該是:

query = (Customers 
     .select() 
     .join(Current_Insurers, on=(Customer.current_insurer == Current_Insurers.id)) 
     .join(Insurers, on=(Current_Insurers.insurer == Insurer.id)) 
     .where(
      (Customers.pol_type == "PC") & 
      (Current_Insurers.effective_date == START_DATE))) 
0

我來到這裏是因爲我有同樣的問題ABD隨後同樣的問題。

我的問題的原因是,當原始插入完成並且python/peewee在稍後更新的謂詞中以毫秒傳遞時,mariaDB正在剝離毫秒。非常令人沮喪。