{"id":13685817,"url":"https://github.com/composite-primary-keys/composite_primary_keys","last_synced_at":"2025-05-11T11:13:59.378Z","repository":{"id":385255,"uuid":"2548","full_name":"composite-primary-keys/composite_primary_keys","owner":"composite-primary-keys","description":"Composite Primary Keys support for Active Record","archived":false,"fork":false,"pushed_at":"2024-12-20T04:10:47.000Z","size":2425,"stargazers_count":1027,"open_issues_count":7,"forks_count":356,"subscribers_count":24,"default_branch":"master","last_synced_at":"2025-05-11T11:13:55.482Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"cmoncrief/morph","license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/composite-primary-keys.png","metadata":{"files":{"readme":"README.rdoc","changelog":"History.rdoc","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2008-03-04T22:58:40.000Z","updated_at":"2025-04-20T20:49:23.000Z","dependencies_parsed_at":"2023-07-05T14:44:51.706Z","dependency_job_id":"b66cc3fc-f931-4816-a7e9-2e93d81a7b11","html_url":"https://github.com/composite-primary-keys/composite_primary_keys","commit_stats":{"total_commits":1214,"total_committers":131,"mean_commits":9.267175572519085,"dds":0.6161449752883031,"last_synced_commit":"70245425050202f3de2b84c67d0b27135885d2f6"},"previous_names":["drnic/composite_primary_keys"],"tags_count":133,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/composite-primary-keys%2Fcomposite_primary_keys","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/composite-primary-keys%2Fcomposite_primary_keys/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/composite-primary-keys%2Fcomposite_primary_keys/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/composite-primary-keys%2Fcomposite_primary_keys/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/composite-primary-keys","download_url":"https://codeload.github.com/composite-primary-keys/composite_primary_keys/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253554120,"owners_count":21926615,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T14:00:57.625Z","updated_at":"2025-05-11T11:13:59.340Z","avatar_url":"https://github.com/composite-primary-keys.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"= Composite Primary Keys for ActiveRecords\n\n== Summary\n\nActiveRecord infamously doesn't support composite primary keys.\nThis gem, composite_primary_keys, or CPK for short, extends ActiveRecord\nto support composite keys.\n\n== Installation\n\n    gem install composite_primary_keys\n\nIf you are using Rails add the following to your Gemfile:\n\n  gem 'composite_primary_keys', '=x.x.x' (see next section about what version to use)\n\n== Versions\n\nEvery major version of ActiveRecord has included numerous internal changes.  As a result,\nCPK has to be rewritten for each version of ActiveRecord.  To help keep\nthings straight, here is the mapping:\n\n    Version 14.x is designed to work with ActiveRecord 7.0.x\n    Version 13.x is designed to work with ActiveRecord 6.1.x\n    Version 12.x is designed to work with ActiveRecord 6.0.x\n    Version 11.x is designed to work with ActiveRecord 5.2.x\n    Version 10.x is designed to work with ActiveRecord 5.1.x\n    Version  9.x is designed to work with ActiveRecord 5.0.x\n    Version  8.x is designed to work with ActiveRecord 4.2.x\n    Version  7.x is designed to work with ActiveRecord 4.1.x\n    Version  6.x is designed to work with ActiveRecord 4.0.x\n    Version  5.x is designed to work with ActiveRecord 3.2.x\n    Version  4.x is designed to work with ActiveRecord 3.1.x\n\nRun the following command to list available versions:\n\n    gem list composite_primary_keys -ra\n\n== The basics\n\nA model with composite primary keys is defined like this:\n\n  class Membership \u003c ActiveRecord::Base\n    self.primary_keys = :user_id, :group_id\n    belongs_to :user\n    belongs_to :group\n    has_many :statuses, :class_name =\u003e 'MembershipStatus', :foreign_key =\u003e [:user_id, :group_id]\n  end\n\nNote the addition of the line:\n\n    self.primary_keys = :user_id, :group_id\n\n\nA model associated with a composite key model is defined like this:\n\n  class MembershipStatus \u003c ActiveRecord::Base\n    belongs_to :membership, :foreign_key =\u003e [:user_id, :group_id]\n  end\n\nThat is, associations can include composite keys too.  All Rails association types are supported. Nice.\n\n== Usage\n\nOnce you’ve created your models to specify composite primary keys (such as the Membership class)\nand associations (such as MembershipStatus#membership), you can use them like any normal model\nwith associations.\n\nBut first, lets check out our primary keys.\n\n  MembershipStatus.primary_key # =\u003e \"id\"    # normal single key\n  Membership.primary_key  # =\u003e [:user_id, :group_id] # composite keys\n  Membership.primary_key.to_s # =\u003e \"user_id,group_id\"\n\nNow we want to be able to find instances using the same syntax we always use for ActiveRecords.\n\n  MembershipStatus.find(1)    # single id returns single instance\n  =\u003e \u003cMembershipStatus:0x392a8c8 @attributes={\"id\"=\u003e\"1\", \"status\"=\u003e\"Active\"}\u003e\n\n  Membership.find([1,1])  # composite ids returns single instance\n  =\u003e \u003cMembership:0x39218b0 @attributes={\"user_id\"=\u003e\"1\", \"group_id\"=\u003e\"1\"}\u003e\n\nNotice the use of an array to specify the composite key values.\n\nNOTE - API CHANGE.  CPK Version 6.x and earlier used to allow composite keys to be listed out\nlike this:\n\n  Membership.find(1,1)\n\nThis usage is no longer supported.\n\n== Databases\n\nCPK supports the following databases:\n\n * PostgreSQL\n * MySQL\n * MariaDB\n * Oracle\n * DB2\n * SQLite\n * SQLServer\n\n== Tests\n\nTo run tests you first need to install the appropriate gems for the database you want to test.  Database gems are\ndivided into the following bundler groups:\n\n * mysql\n * oracle\n * postgresql\n * sqlite\n * sqlserver\n\nSince it is likely you do not have all the above databases installed on your computer, you want to install just the\ngems for your database.  For example, to test postgresql you would install the appropriate gems like this:\n\n  bundler config set --local without \"mysql oracle sqlite sqlserver\"\n  bundler install\n\nOnce you have installed the appropriate gems, the next step is to create the test database. There is a rake\ncommand for each database. Using our example:\n\n  rake postgresql:build_database\n\nYou can also rebuild the database if it already exists using this command:\n\n  rake postgresql:rebuild_database\n\nTo get a list of commands for your database use:\n\n  Rake -T\n\nFinally, to run tests:\n\n  rake postgresql:test\n\nTravis build status: {\u003cimg src=\"https://travis-ci.com/composite-primary-keys/composite_primary_keys.svg\" alt=\"Build Status\" /\u003e}[https://travis-ci.com/composite-primary-keys/composite_primary_keys]\n\n=== DB2\n\nDB2 is no longer supported due to difficulties in getting the ibm_db2 gem to build.  Thus tests\nhave not been run against db2.\n\n=== MariaDb (mysql)\n\nMariaDb is fully supported with all tests passing.\n\n=== Oracle\n\nOracle is fully supported with all tests passing.\n\n=== Postgresql\n\nPostgresql is fully supported with all tests passing.\n\n=== Sqlite 3\n\nThe sqlite database is created at the path composite_primary_keys/db.  Note you must *first* create the database using the\nbuilt-in rake task before running tests:\n\n  rake sqlite:build_database\n\nFor sqlite3 to work correctly, you must manually require 'composite_primary_keys/connection_adapters/sqlite3_adapter' after\nloading the CPK gem.\n\n=== SqlServer\n\nSqlServer is partially supported.  There are a number of failing tests - patches welcomed.\n\n== Questions, Discussion and Contributions\n\nFor help please visit https://github.com/composite-primary-keys/composite_primary_keys.\n\n== Author\n\nFirst version was written by Dr Nic Williams.\n\nMaintained by Charlie Savage\n\nContributions by many!\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcomposite-primary-keys%2Fcomposite_primary_keys","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcomposite-primary-keys%2Fcomposite_primary_keys","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcomposite-primary-keys%2Fcomposite_primary_keys/lists"}