Implementing Native Composite Primary Key Support in Rails 7.1
summary
Rails 7.1 adds native composite primary key support in Active Record with a new query_constraints virtual primary key option, an ID_value attribute to read a lone ID column, and a where by-tuple syntax for bulk loading. Real-world Shopify tests show common selects run 5–6x faster, slow-query volume drops ~80%, while inserts slow ~10x; joint tables and tenant-keyed models benefit most. Associations use composite foreign keys via query_constraints and behave like regular AR associations.
AI-written summary · about
chapters (16)
- 0:00Introduction to composite keys
- 0:55Speaker introduction
- 1:50Fundamentals and use cases
- 3:04Modeling complex relationships
- 4:38Designing database schemas
- 5:39Multitenant application design
- 6:59Data layout and performance
- 8:08Multitenancy performance tradeoffs
- 9:41Sharding and composite keys
- 11:34Summary of key concepts
- 12:08Active Record 7.1 features
- 15:30Defining composite key models
- 17:08Handling the ID column
- 18:25Associations and constraints
- 20:11Virtual primary keys
- 21:41Conclusion and final notes
related talks
21mImplementing Native Composite Primary Key Support in Rails 7.1
21mImplementing Native Composite Primary Key Support in Rails 7.1
59mSQL primary key, surrogate key, composite keys, foreign keys... and JPA
31mRails anti-patterns: how not to design your database
23mSupercharging ActiveRecord with PostreSQL
30mNew and Improved Features in Rails 8.1
37mDenormalizing Rails
33mExperimental Patterns in ActiveRecord
