PostgreSQL“错误:无法确定参数的数据类型”。 Ruby exec_params

发布于 2025-02-07 22:01:49 字数 1482 浏览 2 评论 0原文

我正在尝试执行一个查询,该查询选择从query变量中存储的用户输入中匹配搜索词的配方。这是相关代码的一部分:

class DatabasePersistence
  def initialize(logger)
    @db = if Sinatra::Base.production?
        PG.connect(ENV['DATABASE_URL'])
      else
        PG.connect(dbname: "recipes")
      end
    @logger = logger
  end

  def search_recipes(query)
    p "Query parameter is:"
    p query
    p query.class
    sql = <<~SQL
    SELECT * FROM recipes
    WHERE labels ILIKE '%$1::text%'
    SQL
    results = query(sql, query)
    # ... more code
  end

  def query(statement, *params)
    @logger.info "#{statement}: #{params}"
    @db.exec_params(statement, params)
  end

end

执行此行结果= QUERY(SQL,QUERY)的执行时,会引起以下错误。

PG::IndeterminateDatatype at /search
ERROR: could not determine data type of parameter $1

另一篇帖子类型铸件,这就是为什么我添加类型铸件的原因。我可能会错误地这样做。我还尝试了以下方法:

WHERE labels ILIKE '%text($1)%'
WHERE labels ILIKE '%cast($1 as text)%'
WHERE labels ~ '$1::text'
WHERE labels ~ 'cast($1 as text'

在上述所有情况下,它返回了相同的错误“无法确定参数的数据类型。我添加了一些#p方法调用来确保QUERY debuggin的真实值。

变量是引用 如果我要施放数据类型,而不是 nil 我是错误地传递参数吗? #EXEC_PARAMS方法是否有其他方法可以通过pg.connect类执行的参数?

I am trying to execute a query that selects recipes that match a search term from user input stored in the query variable. This is the portion of relevant code:

class DatabasePersistence
  def initialize(logger)
    @db = if Sinatra::Base.production?
        PG.connect(ENV['DATABASE_URL'])
      else
        PG.connect(dbname: "recipes")
      end
    @logger = logger
  end

  def search_recipes(query)
    p "Query parameter is:"
    p query
    p query.class
    sql = <<~SQL
    SELECT * FROM recipes
    WHERE labels ILIKE '%$1::text%'
    SQL
    results = query(sql, query)
    # ... more code
  end

  def query(statement, *params)
    @logger.info "#{statement}: #{params}"
    @db.exec_params(statement, params)
  end

end

The following error is raised on when this line results = query(sql, query) is executed.

PG::IndeterminateDatatype at /search
ERROR: could not determine data type of parameter $1

Another post suggested adding an explicit type cast which is why I added the type cast. I could be doing it incorrectly. I also tried it like the following:

WHERE labels ILIKE '%text($1)%'
WHERE labels ILIKE '%cast($1 as text)%'
WHERE labels ~ '$1::text'
WHERE labels ~ 'cast($1 as text'

In all of the above cases it returned the same error "could not determine the datatype of parameter. I added some #p method calls to make sure the query variable is referencing a real value for debuggin. I have confirmed that this error occurs when the query references a string object with value oats.

What is causing this error to still occur if I am casting the datatype and it is not nil? Am I passing the parameters incorrectly? Am I casting the parameters incorrectly? Is it possible there is a way to pass datatypes as arguments to the #exec_params method? Is there another way to safely pass parameters to be executed by the instance of the PG.connect class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離人涙 2025-02-14 22:01:50

简而言之:

WHERE labels ILIKE $1::text

我假设标签是一个普通的喜欢文本

Simply:

WHERE labels ILIKE $1::text

I assume labels is a plain character type like text, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文