PostgreSQL“错误:无法确定参数的数据类型”。 Ruby exec_params
我正在尝试执行一个查询,该查询选择从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简而言之:
我假设
标签
是一个普通的喜欢文本
。Simply:
I assume
labels
is a plain character type liketext
, too.