Support tables with compound primary keys #143

Closed
sferik opened this Issue Dec 8, 2010 · 9 comments

Comments

Projects
None yet
6 participants
Owner

sferik commented Dec 8, 2010

No description provided.

Collaborator

kaapa commented Dec 31, 2010

I've started to work on this one in this branch.

kaapa was assigned Apr 10, 2011

Collaborator

bbenezech commented Sep 1, 2011

Composite primary keys are not supported natively in ActiveRecord, why do we want to support this?

mgartner commented Apr 5, 2012

It would be nice to have this now that Mongoid is supported. Compound id's made via the code below break the admin when trying to "show" or "edit" the specific instance.

class Person
  include Mongoid::Document

  field :first_name, :type => String
  field :last_name, :type => String
  key :first_name, :last_name
end

@kaapa how far did you get on this? I'm wondering how much work would this entail, and where would I get started?

Contributor

timkurvers commented Apr 5, 2012

While working on DataMapper ORM support I got an initial draft for composite keys up and running fairly quickly. I managed to get at least the show/edit views to work correctly, haven't progressed far enough to test actual updating.

RailsAdmin seems to assume availability of an :id property on any model instance. That may be true for ActiveRecord or Mongoid but does not hold for DataMapper. This should probably be abstracted away.

In any case, generating a unique id for such instances is the first step. For the model you provided, you'd want Person.id to return a concatenated unique value, say 'Marcus,Gartner'. The Mongoid-adapter's get-method would have to take composite keys into account. For example, by detecting that the model has a composite key and splitting the identifier. Choosing the separator is probably something that should be done in the community, as choosing comma would be disastrous when dealing with key pieces that could potentially hold commas themselves.

mgartner commented Apr 6, 2012

Mongoid already creates a concatenated id when giving it a composite primary key. It uses - as the separator and it downcases all of the letters; for example marcus-gartner.

I'm confused why the get(id) method is failing considering it is doing a simple where() call with Mongoid which should handle composite keys.

Contributor

timkurvers commented Apr 8, 2012

Ah I see, I stand corrected. Thanks for the heads up! Does the actual where() call work in the Rails console - bypassing the abstract model implementation - that is.

mgartner commented Apr 8, 2012

Yes. Something like the following works in the Rails console:

Person.where(:_id => 'marcus-gartner').first

# using find also works in the console:
Person.find('marcus-gartner')
Collaborator

mshibuya commented Apr 10, 2012

FYI: Mongoid's composite primary key support has been removed in master, in favor of overriding the _id field.

Collaborator

bbenezech commented Feb 5, 2016

I don't think RA will ever support this. :( This not supported transparently and directly by the underlying ORM.
Implementation would be very complex. Please add an auto-incremented indexed :id column to your AR models. This is the Rails way, I think.

bbenezech closed this Feb 5, 2016

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