Implementing Native Composite Primary Key Support in Rails 7.1
summary
Rails 7.1 adds native composite primary key support plus an ID_value accessor to read an ID column when the primary key becomes an array, a bulk where by triple query syntax for fetching many composite keys, and a query_constraints association option that acts as a virtual composite foreign key. Real-world use at Shopify shows a ~5× query speedup for orders after prepending tenant key to the PK at the cost of roughly ~10× slower inserts. Join tables and multitenant tables are common winning cases.
AI-written summary · about
chapters (11)
- 0:00Introduction
- 1:24Composite primary keys
- 3:52Join table keys
- 5:16Multitenant apps
- 9:31Tradeoffs of composite keys
- 11:18Sharding similarities
- 13:43Rails 7.1 composite keys
- 16:55Querying composite keys
- 18:18Virtual keys with constraints
- 19:42When to use composite keys
- 20:24Closing
related talks
24mImplementing Native Composite Primary Key Support in Rails 7.1
59mSQL primary key, surrogate key, composite keys, foreign keys... and JPA
23mSupercharging ActiveRecord with PostreSQL
30mNew and Improved Features in Rails 8.1
25mSaaS on Rails on PostgreSQL
42mPowerful Rails Features You Might Not Know
23mHow Shopify Sharded Rails
46mUse PostgreSQL, not Rails, to make Rails Faster!
