Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Support tables with compound primary keys #143

Open
sferik opened this Issue · 8 comments

6 participants

@sferik
Owner

No description provided.

@kaapa
Collaborator

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

@kaapa kaapa was assigned
@bbenezech
Collaborator

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

@mgartner

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?

@timkurvers

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

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.

@timkurvers

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

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')
@mshibuya
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.