简体   繁体   English

ActiveRecord find_by生成不适用于postgres的select语句

[英]ActiveRecord find_by generateing select statement that does not work with postgres

I'm using the Knock gem for JWT authentication in my rails api application. 我在Rails api应用程序中使用Knock gem进行JWT身份验证。 When I create a user with a password everything is fine but then when I hit the route that generates the user_token, I get a 500 error. 当我创建一个使用密码的用户时,一切都很好,但是当我点击生成user_token的路由时,出现500错误。 Knock Gem does an Activerecord find_by on the model being referenced 敲门宝石对所引用的模型执行Activerecord find_by

(this is from https://github.com/nsarno/knock/blob/master/app/controllers/knock/auth_token_controller.rb)
entity_class.find_by email: auth_params[:email]

which triggers this select statement. 触发该选择语句。

User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "test@testuser.ca"], ["LIMIT", 1]]

Unfortunately, the return is a 500 不幸的是,回报是500

I put the select statement into Postico and ran it manually and it still fails. 我将select语句放入Postico并手动运行,但仍然失败。 That is when I discovered that Postgres does not like ( support?) double quotes around values. 那就是当我发现Postgres不喜欢(支持?)值周围的双引号时。 When I changed the email value to single quotes for the email it worked 当我将电子邮件的值更改为电子邮件的单引号时,它起作用了

SELECT  "users".* FROM "users" WHERE "users"."email" = 'no-reply@testuser.ca' LIMIT 1 ;

Double quotes around the column names seems to be fine. 列名周围的双引号似乎很好。

The tricky thing is that this only happens in my production environment that I have set up via a digital ocean droplet. 棘手的是,这仅发生在我通过数字海洋飞沫建立的生产环境中。 I have Capistrano set up to deploy my application. 我已经设置Capistrano来部署我的应用程序。 Locally, the user creation and user_token works perfectly. 在本地,用户创建和user_token非常完美。 I'm not sure why it would work locally when running code, but when I hit my local database with Postico and try the same statement, it crashes as well. 我不确定为什么在运行代码时它可以在本地运行,但是当我用Postico打本地数据库并尝试相同的语句时,它也会崩溃。

Any ideas on how to troubleshoot this or why this is happening would be much appreciated. 任何有关如何解决此问题或为什么发生这种情况的想法将不胜感激。


Log file when it happens 发生日志文件

[1cbc2966-b7e9-4630-a7e9-07ba13818f2d] puma (3.12.0) lib/puma/thread_pool.rb:133:in `block in spawn_thread'
I, [2018-07-30T19:56:43.531575 #29634]  INFO -- : [a11ba95e-d286-4405-8058-7a8594ba07e1] Started GET "/" for xx.xx.xx.xx at 2018-07-30 19:56:43 +0000
I, [2018-07-30T19:56:43.532229 #29634]  INFO -- : [a11ba95e-d286-4405-8058-7a8594ba07e1] Processing by HomeController#index as HTML
I, [2018-07-30T19:56:43.533500 #29634]  INFO -- : [a11ba95e-d286-4405-8058-7a8594ba07e1] [active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (0.07ms)
I, [2018-07-30T19:56:43.533660 #29634]  INFO -- : [a11ba95e-d286-4405-8058-7a8594ba07e1] Completed 200 OK in 1ms (Views: 0.4ms)
I, [2018-07-30T20:03:11.491431 #29634]  INFO -- : [fccff2d7-6398-4ff0-883d-26cc1c02d9f8] Started POST "/users/create" for 184.69.39.226 at 2018-07-30 20:03:11 +0000
I, [2018-07-30T20:03:11.492263 #29634]  INFO -- : [fccff2d7-6398-4ff0-883d-26cc1c02d9f8] Processing by UsersController#create as */*
I, [2018-07-30T20:03:11.492443 #29634]  INFO -- : [fccff2d7-6398-4ff0-883d-26cc1c02d9f8]   Parameters: {"user"=>{"email"=>"test@test.ca", "password"=>"[FILTERED]"}}
D, [2018-07-30T20:03:11.582384 #29634] DEBUG -- : [fccff2d7-6398-4ff0-883d-26cc1c02d9f8]    (0.1ms)  BEGIN
D, [2018-07-30T20:03:11.583996 #29634] DEBUG -- : [fccff2d7-6398-4ff0-883d-26cc1c02d9f8]   User Exists (0.7ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "test@test.ca"], ["LIMIT", 1]]
D, [2018-07-30T20:03:11.585818 #29634] DEBUG -- : [fccff2d7-6398-4ff0-883d-26cc1c02d9f8]   User Create (0.4ms)  INSERT INTO "users" ("email", "password_digest", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["email", "test@test.ca"], ["password_digest", "$2a$10$qyZRU.Sxn9mbPI/AQPMnquyiVFKhX8aG8ClPlYdhxivkPQ5XmX0UO"], ["created_at", "2018-07-30 20:03:11.584946"], ["updated_at", "2018-07-30 20:03:11.584946"]]
D, [2018-07-30T20:03:11.586807 #29634] DEBUG -- : [fccff2d7-6398-4ff0-883d-26cc1c02d9f8]    (0.6ms)  COMMIT
I, [2018-07-30T20:03:11.588042 #29634]  INFO -- : [fccff2d7-6398-4ff0-883d-26cc1c02d9f8] [active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (0.07ms)
I, [2018-07-30T20:03:11.588329 #29634]  INFO -- : [fccff2d7-6398-4ff0-883d-26cc1c02d9f8] Completed 200 OK in 96ms (Views: 1.2ms | ActiveRecord: 5.9ms)
I, [2018-07-30T20:03:15.588367 #29634]  INFO -- : [24372279-3ea9-4f24-b10f-ccd463c3c8b4] Started POST "/user_token" for xx.xx.xx.xx at 2018-07-30 20:03:15 +0000
I, [2018-07-30T20:03:15.589280 #29634]  INFO -- : [24372279-3ea9-4f24-b10f-ccd463c3c8b4] Processing by UserTokenController#create as */*
I, [2018-07-30T20:03:15.589487 #29634]  INFO -- : [24372279-3ea9-4f24-b10f-ccd463c3c8b4]   Parameters: {"auth"=>{"email"=>"test@test.ca", "password"=>"[FILTERED]"}, "user_token"=>{"auth"=>{"email"=>"test@test.ca", "password"=>"[FILTERED]"}}}
D, [2018-07-30T20:03:15.591732 #29634] DEBUG -- : [24372279-3ea9-4f24-b10f-ccd463c3c8b4]   User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "test@test.ca"], ["LIMIT", 1]]
I, [2018-07-30T20:03:15.667905 #29634]  INFO -- : [24372279-3ea9-4f24-b10f-ccd463c3c8b4] Completed 500 Internal Server Error in 78ms (ActiveRecord: 0.4ms)
F, [2018-07-30T20:03:15.669634 #29634] FATAL -- : [24372279-3ea9-4f24-b10f-ccd463c3c8b4]
F, [2018-07-30T20:03:15.669803 #29634] FATAL -- : [24372279-3ea9-4f24-b10f-ccd463c3c8b4] TypeError (no implicit conversion of nil into String):
F, [2018-07-30T20:03:15.669896 #29634] FATAL -- : [24372279-3ea9-4f24-b10f-ccd463c3c8b4]
F, [2018-07-30T20:03:15.669994 #29634] FATAL -- : [24372279-3ea9-4f24-b10f-ccd463c3c8b4] jwt (1.5.6) lib/jwt.rb:75:in `digest'
[24372279-3ea9-4f24-b10f-ccd463c3c8b4] jwt (1.5.6) lib/jwt.rb:75:in `sign_hmac'
[24372279-3ea9-4f24-b10f-ccd463c3c8b4] jwt (1.5.6) lib/jwt.rb:36:in `sign'
[24372279-3ea9-4f24-b10f-ccd463c3c8b4] jwt (1.5.6) lib/jwt.rb:96:in `encoded_signature'
[24372279-3ea9-4f24-b10f-ccd463c3c8b4] jwt (1.5.6) lib/jwt.rb:106:in `encode'
[24372279-3ea9-4f24-b10f-ccd463c3c8b4] knock (2.1.1) app/model/knock/auth_token.rb:14:in `initialize'
[24372279-3ea9-4f24-b10f-ccd463c3c8b4] knock (2.1.1) app/controllers/knock/auth_token_controller.rb:22:in `new'

Further investigation 进一步的调查

I turned my local environment into "production" mode and created the production database locally. 我将本地环境转换为“生产”模式,并在本地创建了生产数据库。 I experience the same 500 internal server error that I do when hitting my VPS that's also in Production mode! 遇到同样处于生产模式的VPS时,我也会遇到500个内部服务器错误! This seems to be a difference between development and production of Active Record and how it works with PostgreSQL? 这似乎是Active Record的开发和生产以及它与PostgreSQL的工作方式之间的区别?

Still trying to figure this out, if anyone has any experience with this please let me know :) 仍在尝试弄清楚这一点,如果有人对此有任何经验,请告诉我:)

The query is fine, and unrelated to your problem. 该查询很好,并且与您的问题无关。

Your log output shows an error that has nothing to do with Postgres: 您的日志输出显示与Postgres无关的错误:

TypeError (no implicit conversion of nil into String):

jwt (1.5.6) lib/jwt.rb:75:in `digest'
jwt (1.5.6) lib/jwt.rb:75:in `sign_hmac'
jwt (1.5.6) lib/jwt.rb:36:in `sign'
jwt (1.5.6) lib/jwt.rb:96:in `encoded_signature'
jwt (1.5.6) lib/jwt.rb:106:in `encode'
knock (2.1.1) app/model/knock/auth_token.rb:14:in `initialize'
knock (2.1.1) app/controllers/knock/auth_token_controller.rb:22:in `new'

The issue seems to have stemmed from Rails no longer using secrets but credentials in 5.2 这个问题似乎源于Rails不再使用机密,而是使用5.2中的凭据

Here is the github issue for Knock https://github.com/nsarno/knock/issues/205 这是Knock的github问题https://github.com/nsarno/knock/issues/205

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM