{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/26541","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/26541/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/26541/comments","events_url":"https://api.github.com/repos/rails/rails/issues/26541/events","html_url":"https://github.com/rails/rails/issues/26541","id":177660423,"number":26541,"title":"PostgreSQL bind parameter getting lost for subquery's LIMIT","user":{"login":"Envek","id":264400,"avatar_url":"https://avatars.githubusercontent.com/u/264400?v=3","gravatar_id":"","url":"https://api.github.com/users/Envek","html_url":"https://github.com/Envek","followers_url":"https://api.github.com/users/Envek/followers","following_url":"https://api.github.com/users/Envek/following{/other_user}","gists_url":"https://api.github.com/users/Envek/gists{/gist_id}","starred_url":"https://api.github.com/users/Envek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Envek/subscriptions","organizations_url":"https://api.github.com/users/Envek/orgs","repos_url":"https://api.github.com/users/Envek/repos","events_url":"https://api.github.com/users/Envek/events{/privacy}","received_events_url":"https://api.github.com/users/Envek/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-09-18T16:10:19Z","updated_at":"2016-09-20T05:56:57Z","closed_at":null,"body":"### Steps to reproduce\r\n\r\nSee the gist: https://gist.github.com/Envek/fbd1f8576148ec4be33308636339b314\r\n\r\nModels:\r\n\r\n```ruby\r\nclass Post < ActiveRecord::Base\r\n  has_many :comments\r\nend\r\n\r\nclass Comment < ActiveRecord::Base\r\n  belongs_to :post\r\nend\r\n```\r\nand query:\r\n\r\n```ruby\r\nlast_comment_relation = Comment.where('\"comments\".\"post_id\" = \"posts\".\"id\"').order(created_at: :desc).limit(1)\r\nposts = Post.all.eager_load(:comments).where(comments: { id: [nil, last_comment_relation.select(:id)] }).load\r\n```\r\n\r\n### Expected behavior\r\nQuery should run successfully and should return all posts with 0 or 1 latest comment for each with single query to DB\r\n\r\n### Actual behavior\r\n\r\nException\r\n\r\n    ERROR: bind message supplies 0 parameters, but prepared statement \"\" requires 1\r\n\r\nWhile executing:\r\n\r\n```ruby\r\nSELECT \"posts\".\"id\" AS t0_r0, \"comments\".\"id\" AS t1_r0, \"comments\".\"post_id\" AS t1_r1, \"comments\".\"created_at\" AS t1_r2, \"comments\".\"updated_at\" AS t1_r3 FROM \"posts\" LEFT OUTER JOIN \"comments\" ON \"comments\".\"post_id\" = \"posts\".\"id\" WHERE (\"comments\".\"id\" IN (SELECT  \"comments\".\"id\" FROM \"comments\" WHERE (\"comments\".\"post_id\" = \"posts\".\"id\") ORDER BY \"comments\".\"created_at\" DESC LIMIT $1) OR \"comments\".\"id\" IS NULL)\r\n```\r\n\r\nThis error raises only when I add check for `NULL` as inclusion of `comments.id` in array of `nil` and subquery relation and only if that relation contains limit:\r\n\r\n```ruby\r\nwhere(comments: { id: [nil, last_comment_relation.select(:id)] })\r\n```\r\n\r\nNeither #25786 or #25877 doesn't seem to fix this issue\r\n\r\n### System configuration\r\n**Rails version**: 5.0.0.1\r\n\r\n**Ruby version**: 2.3.1\r\n"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/248208801","html_url":"https://github.com/rails/rails/issues/26541#issuecomment-248208801","issue_url":"https://api.github.com/repos/rails/rails/issues/26541","id":248208801,"user":{"login":"girishso","id":35009,"avatar_url":"https://avatars.githubusercontent.com/u/35009?v=3","gravatar_id":"","url":"https://api.github.com/users/girishso","html_url":"https://github.com/girishso","followers_url":"https://api.github.com/users/girishso/followers","following_url":"https://api.github.com/users/girishso/following{/other_user}","gists_url":"https://api.github.com/users/girishso/gists{/gist_id}","starred_url":"https://api.github.com/users/girishso/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/girishso/subscriptions","organizations_url":"https://api.github.com/users/girishso/orgs","repos_url":"https://api.github.com/users/girishso/repos","events_url":"https://api.github.com/users/girishso/events{/privacy}","received_events_url":"https://api.github.com/users/girishso/received_events","type":"User","site_admin":false},"created_at":"2016-09-20T05:56:57Z","updated_at":"2016-09-20T05:56:57Z","body":"This happens with sqlite3 as well, nothing specific to postgres."}}
{"actor":{"display_login":"girishso"}}