#765 incomplete
Shawn

primary_key option for belongs_to

Reported by Shawn | August 6th, 2008 @ 04:40 PM | in 2.x

Could some one add :primary_key option into belongs_to in a similar fashion like ticket 292? This option is a nice-to-have for has_many, because you can always use finder_sql to workaround it. But it is a must-to-have for belongs_to because there is no way to wrok around it. I had a quick look at the source code and added

def owner_quoted_id

if @reflection.options:primary_key]

quote_value(@owner.send @reflection.options:primary_key]))

else

@owner.quoted_id

end

end

into belongs_to.rb.

it didn't work

Comments and changes to this ticket

  • Philip Hallstrom

    Philip Hallstrom August 12th, 2008 @ 07:12 PM

    • → Tag changed from “” to “patch”

    I've written up a patch that seems to work. I may not be thinking it all the way through, but maybe it will get things closer to where they need to be. I've done limited testing (no unit tests that is), but it is working for me.

    Also, 'rake test_sqlite3' in the activerecord directory still passes all tests.

    
    class Occupation < ActiveRecord::Base
    
      has_many :aliases, :class_name => 'OccupationAlias', :primary_key => :onet_soc_code, :foreign_key => :onet_soc_code
    
    end
    
    
    class OccupationAlias < ActiveRecord::Base
    
      belongs_to :occupation, :primary_key => :onet_soc_code, :foreign_key => :onet_soc_code
    
    end
    
    
    $ ./script/runner "o = Occupation.find(:first); puts o.id; puts OccupationAlias.find(o.aliases.first.id).occupation.id"
    3797
    3797
    

    The SQL run is:

    
      Occupation Load (0.002571)   SELECT * FROM "occupations" LIMIT 1
      OccupationAlias Load (0.034346)   SELECT * FROM "occupation_aliases" WHERE ("occupation_aliases".onet_soc_code = E'11-1011.00') ORDER BY title LIMIT 1
      OccupationAlias Load (0.000405)   SELECT * FROM "occupation_aliases" WHERE ("occupation_aliases"."id" = 102190)
      Occupation Load (0.000505)   SELECT * FROM "occupations" WHERE ("occupations"."onet_soc_code" = E'11-1011.00') LIMIT 1
    

    Patch is attached.

  • Philip Hallstrom

    Philip Hallstrom August 12th, 2008 @ 07:26 PM

    I dug into the unit tests and believe I've added a correct unit test to test this functionality.

    
    $ rake test_sqlite3 TEST=test/cases/associations/belongs_to_associations_test.rb
    (in /Users/philip/Work/Careers.org/web/vendor/rails/activerecord)
    /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -Ilib:test:test/connections/native_sqlite3 "/Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader.rb" "test/cases/associations/belongs_to_associations_test.rb"
    Using native SQLite3
    Loaded suite /Library/Ruby/Gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader
    Started
    ....................................
    Finished in 0.472048 seconds.
    
    36 tests, 123 assertions, 0 failures, 0 errors
    

    Original patch with unit tests is attached.

  • Philip Hallstrom

    Philip Hallstrom August 15th, 2008 @ 06:21 AM

    • → Title changed from “:primary_key option for belongs_to ” to “PATCH: primary_key option for belongs_to ”
    • → Tag changed from “patch” to “activerecord belongs_to patch tests”
  • Pratik

    Pratik August 16th, 2008 @ 08:32 PM

    • → Title changed from “PATCH: primary_key option for belongs_to ” to “primary_key option for belongs_to ”
  • Philip Hallstrom

    Philip Hallstrom August 18th, 2008 @ 09:52 PM

    Same patch with tests in format correct git/rails format for submission.

  • Philip Hallstrom

    Philip Hallstrom August 19th, 2008 @ 05:57 AM

    This patch doesn't cover creation (State.cities.build(...)) correctly.

  • Philip Hallstrom

    Philip Hallstrom August 19th, 2008 @ 06:38 AM

    Updated patch to fix association build/create.

  • Frederick Cheung

    Frederick Cheung August 21st, 2008 @ 02:20 AM

    A few comments:

    *

    
    owner_id = @reflection.options.has_key?(:primary_key) ? @owner.send(@reflection.options[:primary_key]) : @owner.id
    

    might be a bit neater as

    
    owner_id = @owner.send(@reflection.options[:primary_key] || :id)
    
    • You'll need to handle eager & preloading
    • the find_by bit in find_target looks a bit grungy
  • Jeremy Kemper

    Jeremy Kemper August 28th, 2008 @ 07:33 AM

    • → State changed from “new” to “incomplete”
    • → Milestone changed from “2.x” to “2.2”
  • logan

    logan September 3rd, 2008 @ 09:00 PM

    Just wanted to add some additional motivation for this patch. I'm using uuids in a multi-machine, multi-database configuration with ActiveResource. Since objects are moved between databases on each machine, they can't use the autoincrementing 'id' column, hence uuids. Changing the database primary key to be a uuid has a negative impact on Innodb performance so this patch is the right solution.

    Is there a workaround or patch available for has_and_belongs_to_many?

  • Zyclops

    Zyclops October 16th, 2008 @ 01:32 AM

    This is a must have feature for us. We are using the finder_sql for the has_many relationship but need this to get the belongs_to to work.

    We currently need to regularly import data from a legacy system and combine them with additional records from the system. Using a separate key would solve the issue.

  • Jeremy Kemper

    Jeremy Kemper October 16th, 2008 @ 03:02 AM

    Complete the patch and the feature will go into Rails. It's that easy :)

  • Pratik

    Pratik October 17th, 2008 @ 05:18 PM

    • → Milestone changed from “2.2” to “2.x”
    • → Assigned user changed from “Jeremy Kemper” to “Pratik”

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins