[英]Why is ActiveRecord/PostgreSQL not finding my data in the database?
I'm sure this is something simple I'm overlooking but since I've been dealing with this strange issue for a few days now I'm asking for help. 我敢肯定,这是我所忽略的简单事情,但是由于我已经处理了几天这个奇怪的问题,所以我正在寻求帮助。
A Rails 3.2.13 app with the pg gem and the following db scheme: 一个带有pg gem和以下数据库方案的Rails 3.2.13应用程序:
create_table "clients", :force => true do |t|
t.string "uuid"
t.string "private_key"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "clients", ["private_key"], :name => "index_clients_on_private_key"
add_index "clients", ["uuid"], :name => "index_clients_on_uuid"
When I perform a simple "SELECT * FROM clients;" 当我执行一个简单的“ SELECT * FROM clients”时; using the RubyMine database console I get the rows pictured in the screen shot above. 使用RubyMine数据库控制台,我得到了上面屏幕截图中所示的行。 When I perform a "SELECT * FROM clients WHERE id = 11;" 当我执行“ SELECT * FROM client where id = 11;”时 I get that row, all is well there. 我得到那一行,一切都很好。 However, the issue is that when I try to perform a "SELECT * FROM clients WHERE private_key = "MkRBNUZBQzUtMzVDRi00NzQ3LThFNjEtNjI4OThERUQzQkRF";" 但是,问题是当我尝试执行“ SELECT * FROM client WHERE private_key =“ MkRBNUZBQzUtMzVDRi00NzQ3LThFNjEtNjI4OThERUQzQkRF”;“ it fails and shows the following in the console output... 它失败并在控制台输出中显示以下内容...
sql> SELECT * FROM clients WHERE private_key = "MkRBNUZBQzUtMzVDRi00NzQ3LThFNjEtNjI4OThERUQzQkRF"
[2013-04-17 20:09:51] [42703] ERROR: column "MkRBNUZBQzUtMzVDRi00NzQ3LThFNjEtNjI4OThERUQzQkRF" does not exist
Position: 43
I've also tried pulling this row out of the db by utilizing the active record helper methods... 我还尝试通过使用活动记录助手方法将这一行从数据库中拉出...
Client.exists?(:private_key => token)
Client.find_by_private_key(token)
However, nothing works, it never finds the record that does exist in the db. 但是,没有任何效果,它永远找不到数据库中确实存在的记录。 Does anyone have any idea what is going on here? 有人知道这里发生了什么吗? All I need to be able to do is utilize the active record helpers, specifically the "Client.exists?(:private_key => token)" to be able to check and see if the supplied token exists in the db. 我需要做的就是利用活动记录助手,尤其是“ Client.exists?(:private_key =>令牌)”来检查并查看提供的令牌是否存在于数据库中。
Thanks for your help! 谢谢你的帮助!
I just tried Client.find(12)
just to see if it would find the record by id, and this works. 我只是尝试Client.find(12)
只是为了查看它是否可以通过id查找记录,因此可以正常工作。 This doesn't help me understand why I still can't use Client.exists?(:private_key => token)
or Client.find_by_private_key(token)
. 这不能帮助我理解为什么我仍然不能使用Client.exists?(:private_key => token)
或Client.find_by_private_key(token)
。
The answer to this problem was that the code generating my private_key value was adding a return/white space to the end of the value. 该问题的答案是,生成我的private_key值的代码在该值的末尾添加了一个返回/空格。 So when trying to query the db for row based on a private_key it was always failing to find anything. 因此,当尝试基于private_key在数据库中查询行时,总是找不到任何东西。
To fix the problem I added a gsub to the end of the code that generates the private_key, like this... 为了解决该问题,我在生成private_key的代码末尾添加了gsub,如下所示:
private_key = SecurityHelper.encrypt_private_key(client_uuid).gsub(/\s+/, "")
This strips all white space from the generated private key and solved the problem. 这会从生成的私钥中删除所有空白,并解决了问题。
You're using the wrong quoting style. 您使用了错误的引用样式。 ANSI SQL quoting as used in PostgreSQL is: PostgreSQL中使用的ANSI SQL引用是:
"SELECT * FROM clients WHERE private_key = 'MkRBNUZBQzUtMzVDRi00NzQ3LThFNjEtNjI4OThERUQzQkRF';"
Note the single quotes. 请注意单引号。
It isn't clear how the first statement even got parsed by Ruby, since the inner double quotes would've ended the quoted string, leading to: 还不清楚第一个语句是如何被Ruby解析的,因为内部双引号会结束带引号的字符串,从而导致:
"SELECT * FROM clients WHERE private_key = "MkRBNUZBQzUtMzVDRi00NzQ3LThFNjEtNjI4OThERUQzQkRF";"
^^^^
should be Ruby syntax error here
I don't really do Rails so I can't speak for the correct Rails syntax to do this check via ActiveRecord, though. 我并不是真的做Rails,所以我不能说正确的Rails语法来通过ActiveRecord进行此检查。
你有没有尝试过
Client.exists?(:private_key => "MkRBNUZBQzUtMzVDRi00NzQ3LThFNjEtNjI4OThERUQzQkRF")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.