Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveRecord prepared statement problem with has_many through with foreign_key #12852

Closed
Robinson7D opened this issue Nov 11, 2013 · 5 comments
Closed

Comments

@Robinson7D
Copy link

I'm getting the following message when I attempt to use a has_many through with a defined foreign key:

ActiveRecord::StatementInvalid (PG::ProtocolViolation: ERROR:  bind message supplies 2 parameters, but prepared statement "a136" requires 1
: SELECT "social_feeds".* FROM "social_feeds" INNER JOIN "dashboard_tab_feeds" ON "social_feeds"."id" = "dashboard_tab_feeds"."social_feed_id" WHERE "dashboard_tab_feeds"."social_feed_id" = 447 AND "dashboard_tab_feeds"."dashboard_tab_id" = $1):

The query is simply:

# Where tab has already been found, is a DashboardTab:
tab.social_feeds.to_a 

The models are structured as follows:

class DashboardTab < ActiveRecord::Base
  has_many :dashboard_tab_feeds, foreign_key: :dashboard_tab_id, dependent: :destroy
  has_many :social_feeds, through: :dashboard_tab_feeds
end

class DashboardTabFeed < ActiveRecord::Base
  belongs_to :social_feed
  belongs_to :dashboard_tab
end

class SocialFeed < ActiveRecord::Base
  has_many :dashboard_tab_feeds, foreign_key: :social_feed_id, dependent: :destroy
end

This is a trivial example where the foreign keys do not need to be explicitly stated - Rails magic will take care of that for me in this instance - so I can simply remove the foreign_key specifier on the DashboardTab has_many statement and everything works.

This bug seems to be specific to Rails 4.0.1, does not occur on Rails 4.0.0

@senny
Copy link
Member

senny commented Nov 11, 2013

@Robinson7D can you write an executable test-case to reproduce? You can use this script as a foundation.

@vipulnsward
Copy link
Member

@Robinson7D @senny I can't seem to reproduce this on master or 4.0.1. at https://gist.github.com/vipulnsward/76a5404d2d4a36a2ec21
@Robinson7D is there some specific call sequence?

@senny
Copy link
Member

senny commented Dec 1, 2013

@vipulnsward never mind reading the issue again reveals that it's about the :foreign_key option not a real foreign key. Sorry for the noise.

@Robinson7D
Copy link
Author

Seems like it's working for every test I attempt to create. Perhaps there is an issue with our setup on this project. Vipul's test looks as accurate as can be made to me (and essentially mirrors the version I wrote this weekend based off the test script).

Anyway, since the tests are all passing, and I cannot make them fail, I'll close this ticket.

@vipulnsward
Copy link
Member

Thanks @Robinson7D .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants