Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

belongs_to with :primary_key does not work with find's :include

#3208

Since 2.3.3 Rails supports both :foreign_key and :primary_key for belongs_to association.
Using both (or only :primary_key) works great for simple instance.owner.
However, when using Owned.find(..., :include => [:owner]) the owner gets joined using it's ID (model's own primary key) column instead of the :primary_key defined within Owned's (which is the expected behaviour) belongs_to :owner association.

Thus
owned.owner yields different results based on whether owned was pulled using Owned.find(id, :include => [:owner]) or Owned.find(id) (and of course .owner gets pulled when owned.owner gets called in the view).

It was pretty hard to stumble upon this error, as one needs to have some overlapping values in both columns. In our case it was
regions table has column "zip" (5-digit numbers)
users table has column "postal_code", User belongs_to(:region, :foreign_key => :postal_code, :primary_key => :zip)
It turned out that when using @user=User.find(id, :include => [:region]) the region was joined using regions.id instead of regions.zip:

SELECT "users".* FROM "users" LEFT OUTER JOIN "regions" ON "regions".id = "users".postal_code WHERE ("users"."id" = 1))

Reported by Tomash · September 15th, 2009 @ 11:01 AM

State: duplicate
Milestone: 2.3.9
Assigned to: nobody
Importance: Medium

Activity

  1. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2.3.3, :include, associations, belongs_to to 2.3.3, :include, associations, belongs_to, bugmash

    September 25th, 2009 @ 12:09 PM

  2. Matt Mitchell
    Matt Mitchell

    I'm uploading a patch here. The JoinAssociation.associtation_join method now first checks for the reflection.options[:primary_key] and then defaults to reflection.klass.primary_key if the option is missing. This is my first patch so please let me know if this isn't the way to do it.

    September 29th, 2009 @ 07:07 PM

  3. Deleted User
    Deleted User
    • Tag changed from 2.3.3, :include, associations, belongs_to, bugmash to 2, :include, associations, belongs_to, bugmash

    I'm using Rails 2.3.4, and I'm seeing this behaviour too.

    December 15th, 2009 @ 08:24 AM

  4. Yaroslav Markin
    Yaroslav Markin
    • Assigned user set to Frederick Cheung

    Rails 2.3.5, before the patch I was having horrible problems with ThinkingSphinx indexing belongs_to association with :primary_key. Applied it, works just fine.

    February 11th, 2010 @ 12:44 PM

  5. Alexey Nayden
    Alexey Nayden

    The bug confirmed at 2.3.5. However patch works fine.

    February 11th, 2010 @ 12:47 PM

  6. Rizwan Reza
    Rizwan Reza
    • Tag changed from 2, :include, associations, belongs_to, bugmash to 2, :include, associations, belongs_to

    February 12th, 2010 @ 12:46 PM

  7. humanzz
    humanzz

    I just stumbled upon this bug in Rails 2.3.5 too :(

    April 12th, 2010 @ 04:17 PM

  8. Yaroslav Markin
    Yaroslav Markin
    • Tag changed from 2, :include, associations, belongs_to to 2, :include, activerecord, associations, belongs_to, bug, patch
    • Assigned user changed from Frederick Cheung to José Valim

    Requesting committer attention :)

    April 12th, 2010 @ 04:20 PM

  9. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 2.3.6
    • State changed from new to incomplete

    Needs a test.

    April 24th, 2010 @ 10:27 PM

  10. José Valim
    José Valim
    • Assigned user changed from José Valim to Jeremy Kemper

    April 29th, 2010 @ 11:53 AM

  11. Jeremy Kemper
  12. José Valim
    José Valim
    • Tag changed from 2, :include, activerecord, associations, belongs_to, bug, patch to 2, :include, activerecord, associations, belongs_to, bug, bugmash, patch

    April 30th, 2010 @ 03:17 PM

  13. Łukasz Strzałkowski
    Łukasz Strzałkowski

    Jeremy, isn't that fixed and tested with this commit?

    http://github.com/rails/rails/commit/00b95eb265ff606ae0a7b650265aa3...

    Personally I think the test is week.

    May 5th, 2010 @ 07:38 PM

  14. Jeremy Kemper
    Jeremy Kemper

    Definitely needs a better test.

    May 5th, 2010 @ 08:08 PM

  15. Ernie Miller
    Ernie Miller

    I'll cook something better up.

    May 5th, 2010 @ 08:35 PM

  16. Łukasz Strzałkowski
  17. Ernie Miller
  18. Ernie Miller
    Ernie Miller
    • Tag changed from 2, :include, activerecord, associations, belongs_to, bug, bugmash, patch to 2, :include, activerecord, associations, belongs_to, bug, bugmash, patch, tests

    May 5th, 2010 @ 11:35 PM

  19. Rizwan Reza
  20. HBpencil
    HBpencil
    • Assigned user cleared.

    try this

    class Region
    set_primary_key :zip end

    May 16th, 2010 @ 07:12 AM

  21. HBpencil
    HBpencil

    try this

    class Region
      set_primary_key :zip 
    end
    

    May 16th, 2010 @ 07:13 AM

  22. Anil Wadghule
    Anil Wadghule

    +1

    Verified both patches of tests on rails 3 master and 2-3 stable. HBpencil what are you trying to say? If possible more tests would be good.

    May 16th, 2010 @ 12:24 PM

  23. Rizwan Reza
    Rizwan Reza
    • Tag changed from 2, :include, activerecord, associations, belongs_to, bug, bugmash, patch, tests to 2, :include, activerecord, associations, belongs_to, bug, bugmash-review, patch, tests
    • State changed from incomplete to verified

    May 16th, 2010 @ 04:59 PM

  24. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.6 to 2.3.7

    May 23rd, 2010 @ 05:54 PM

  25. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.7 to 2.3.8

    May 24th, 2010 @ 09:40 AM

  26. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.8 to 2.3.9

    May 25th, 2010 @ 11:45 PM

  27. Łukasz Strzałkowski
    Łukasz Strzałkowski
    • Importance changed from to Medium

    This ticket actually can be closed. It's duplicate of #765 PATCH: primary_key option for belongs_to

    and this is solved by this commit: http://github.com/rails/rails/commit/63026541b209cc11ffd74cf3ca04b8...

    June 28th, 2010 @ 06:58 PM

  28. José Valim
    José Valim
    • State changed from verified to duplicate

    Awesome, thanks!

    June 28th, 2010 @ 06:59 PM

  29. Andrea Campi
    Andrea Campi
    • Tag changed from 2, :include, activerecord, associations, belongs_to, bug, bugmash-review, patch, tests to 2-3-stable, :include, activerecord, associations, belongs_to, bug, bugmash-review, patch, tests

    October 16th, 2010 @ 11:40 PM