The composite_primary_keys gem has been around since 2006 and has done a pretty good job keeping up with Rails versions.
(aside: There are plenty of old threads on the same topic, but I didn’t see anything recently, nor any clear stand that Rails core is taking on the matter)
In latest Rails, the surface area to adjust seems pretty small.
Sometimes, there are good reasons to use composite primary keys. For example
I have a Job which belongs to a Project and to a Customer.
There’s a join-table entity called ProjectCustomer. It has an auto-increment primary key and a unicity constraint on project_id and customer_id.
To make it possible to define a non-wonky and eager-loadable Job has_one :project_customer, foreign_key: [:project_id, :customer_id], primary_key: [:project_id, :customer_id]
all I need to do is install the gem. I don’t even need to define ProjectCustomer self.primary_keys = :project_id, :customer_id
.
This a very reasonable and non-fancy use-case, I think, without which, I’d need do something like has_one :project_customer, through: :project, ->(record) { where(customer: record.customer) }
which is not great.
related code is something like a few small areas in
- rails/active_record_migrations.md at bc9fa030a62a1a65d383dc08bc7de0255e555450 · rails/rails · GitHub
- rails/primary_key.rb at 18707ab17fa492eb25ad2e8f9818a320dc20b823 · rails/rails · GitHub
- rails/database_statements.rb at 7fe221d898afe877f282b549bd8bb908ebe1843d · rails/rails · GitHub
- rails/has_and_belongs_to_many.rb at f95c0b7e96eb36bc3efc0c5beffbb9e84ea664e4 · rails/rails · GitHub
Is it time? Practically speaking, there are already maintainers of the feature…