diff options
author | Nerian <siotopo@gmail.com> | 2017-05-18 18:55:50 +0200 |
---|---|---|
committer | Nerian <siotopo@gmail.com> | 2017-05-18 18:55:50 +0200 |
commit | 3ac5229f32433405628dbca0ec190f7df5013841 (patch) | |
tree | 240cc6bfc3e3055eb8dec5079eb40341e62f0690 | |
parent | 75fa8dd309a84e125b59d01bf182d88419631eaa (diff) | |
download | rails-3ac5229f32433405628dbca0ec190f7df5013841.tar.gz rails-3ac5229f32433405628dbca0ec190f7df5013841.tar.bz2 rails-3ac5229f32433405628dbca0ec190f7df5013841.zip |
Document support for composite primary keys
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 13629dee7f..6015c38ab5 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -188,6 +188,8 @@ module ActiveRecord # The name of the primary key, if one is to be added automatically. # Defaults to +id+. If <tt>:id</tt> is false, then this option is ignored. # + # If an array is passed, a composite primary key will created. + # # Note that Active Record models will automatically detect their # primary key. This can be avoided by using # {self.primary_key=}[rdoc-ref:AttributeMethods::PrimaryKey::ClassMethods#primary_key=] on the model @@ -241,6 +243,23 @@ module ActiveRecord # label varchar # ) # + # ====== Create a composite primary key + # + # create_table(:orders, primary_key: [:product_id, :client_id]) do |t| + # t.belongs_to :product + # t.belongs_to :client + # end + # + # generates: + # + # CREATE TABLE order ( + # product_id integer NOT NULL, + # client_id integer NOT NULL + # ); + # + # ALTER TABLE ONLY "orders" + # ADD CONSTRAINT orders_pkey PRIMARY KEY (product_id, client_id); + # # ====== Do not add a primary key column # # create_table(:categories_suppliers, id: false) do |t| |