【问题标题】:SQL statement working in Sqlite and not working in PostgreSQL why?SQL 语句在 Sqlite 中工作而在 PostgreSQL 中不工作,为什么?
【发布时间】:2017-08-19 00:29:27
【问题描述】:
Showing /app/app/views/admin/business/_business_update_form.html.erb where line #31 raised:

PG::UndefinedFunction: ERROR:  operator does not exist: character varying = integer

LINE 1: SELECT name, id FROM categories WHERE parent = 1
                                                     ^

HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

: SELECT name, id FROM categories WHERE parent = 1
Extracted source (around line #10):

parent = object.category.parent
    sql = "SELECT name, id FROM categories WHERE parent = " + parent.to_s
    Category.find_by_sql(sql)

end

任何解决方案。请帮忙。

【问题讨论】:

  • 错误信息说明这里有什么问题。您的列类型和变量类型应该匹配。像这样:... WHERE parent = 1::character varying

标签: ruby-on-rails postgresql heroku


【解决方案1】:

最好使用 Rails 的方法来处理 ActiveRecord。这样一来,Rails 创建了可以在任何数据库(至少是 sqlite、pg 或 mysql)上运行的数据库查询

parent = object.category.parent
Category.where(parent: parent).select(:id, :name)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    相关资源
    最近更新 更多