2015-02-07 52 views
0

我有我必須編寫SQL查詢的web2py應用程序。有一張名爲作業的表格,其中我感興趣的列是接收器。此欄包含已收到作業的學生列表。 這列由web2py錯誤:您可能需要添加明確的類型轉換

創建我需要將這些列表值在另一個表比較列學生稱爲student_guardian_relation

db.define_table('student_guardian_relation', 
Field('student',db.auth_user, requires=IS_NULL_OR(IS_IN_DB(db(db.auth_user.user_role == UserRoles.Student),db.auth_user.id,'%(first_name)s %(last_name)s'))), 
Field('guardian',db.auth_user, requires=IS_NULL_OR(IS_IN_DB(db(db.auth_user.user_role == UserRoles.Guardian),db.auth_user.id,'%(first_name)s %(last_name)s'))) 
) 

但是,當我嘗試這兩個值進行比較,我得到一個錯誤,多數民衆贊成寫着:

< class 'psycopg2.ProgrammingError'> operator does not exist: text = integer LINE 1: ...rdian_relation, homework WHERE (homework.receiver = student_...^HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

這是我的代碼呢:

print db(db.homework.receiver == db.student_guardian_relation.student).select(db.homework.receiver) 

附加信息:

當我寫:

print db().select(db.homework.receiver) 

我得到了如圖所示的接收器列表。 任何幫助將不勝感激。 謝謝!

enter image description here

+0

DB中的某些東西是一個int,而某些東西是一個字符串,並且postgres告訴你它不能比較這兩個東西......很難判斷哪些是不能看到更多的代碼, - 你可以發佈實際的模型類嗎? – kylewm 2015-02-07 07:20:22

+0

接收者是一個列表,我想將它與int中的學生ID進行比較。 – MJB 2015-02-07 08:04:54

回答

1

請注意,db.homework.receiverlist:reference類型字段,因此它存儲記錄標識列表,而不是單個標識。因此,您必須使用.contains運營商而不是==來檢查列表中是否包含特定ID。

在內部,list:reference字段以文本字段的形式存儲在「|」字符定界ID(例如,「| 1 | 5 | 12 | 32 |」)。 web2py DAL自動處理這種格式和Python列表之間的轉換。無論如何,這就是您使用==運算符觸發類型不匹配錯誤(即您的查詢將字符串與整數進行比較)的原因。

欲瞭解更多詳情,請查閱相關documentation

+0

謝謝你的解釋。現在我明白爲什麼我會收到錯誤以及爲什麼使用.contains修復它。 – MJB 2015-02-10 07:31:51

0

我終於解決了涉及與INT比較行的問題。我用

db((db.homework.receiver.contains(db.student_guardian_relation.student)) 

我覺得不管類型的比較,這兩個表中的值(在我的情況下,至少它像是做到這一點),你可以通過建立在這一個進一步擴展您的查詢。

相關問題