Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Support tables with compound primary keys #143

sferik opened this Issue · 8 comments

6 participants


No description provided.


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

@kaapa kaapa was assigned

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


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

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


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 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.


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.


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.


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

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

# using find also works in the console:

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.