在Postgresql中改变类型..如何正确地做到这一点?

时间:2011-01-19 17:02:54

标签: sql ruby-on-rails postgresql comparison

我收到此错误,但我不完全确定如何编写它以使其满足postgresql ..

ActionView::Template::Error (PGError: ERROR:  operator does not exist: character varying == unknown
LINE 1: ...ed_at > '2011-01-19 16:05:18.738413' AND category == 'humor'...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT     "posts".* FROM       "posts"  WHERE     (created_at > '2011-01-19 16:05:18.738413' AND category == 'humor')):
    77:             - for category in @categories
    78:               %li
    79:                 .new_posts
    80:                   - if current_user && !current_user.last_logins.select{|ll|ll.category_id == category.id}.empty? && Post.find(:all, :conditions => ["created_at > ? AND category == ?", current_user.last_logins.find_by_category_id(category).updated_at, category.name]).size > 0

如果这很模糊,我道歉,但我不确定自己是否理解。

我认为发生的事情是将varchar与整数进行比较?因为我不能合理地机会我的sql表,有没有办法绕过这个?

2 个答案:

答案 0 :(得分:7)

您可能想要使用单个等号(=)。不幸的是,在SQL land中,=是对相等性的测试,而不是==。

答案 1 :(得分:3)

Postgres不使用双等号运算符,它使用单个。

所以改变这个:

:conditions => ["created_at > ? AND category == ?", current_user.last_logins.find_by_category_id(category).updated_at, category.name]

为:

:conditions => ["created_at > ? AND category = ?", current_user.last_logins.find_by_category_id(category).updated_at, category.name]