class ActiveRecord::ConnectionAdapters::PostgreSQLPrimaryKeyConstraint
Creates PRIMARY KEY constraints for PostgreSQL tables and columns.
This class is meant to be used by PostgreSQL column and table definition and manipulation methods. There are several ways to create a PRIMARY KEY constraint:
-
on a table definition
-
on a column definition
-
when altering a table
ActiveRecord
itself already provides some methods of creating PRIMARY KEYS, but we’ve added some PostgreSQL-specific extensions here. To override ActiveRecord’s built-in PRIMARY KEY generation, add an option for :id => false
when creating the table via create_table
.
When creating PRIMARY KEYs, you can use an options Hash to add various PostgreSQL-specific options as necessary or simply use a true statement to create a PRIMARY KEY on the column. Composite PRIMARY KEYs can also be created across multiple columns using a table definition or the PostgreSQLAdapter#add_primary_key
method.
Column Definition¶ ↑
When creating a new table via PostgreSQLAdapter#create_table
, you can specify PRIMARY KEY constraints on individual columns during definition.
Examples¶ ↑
create_table(:foo, :id => false) do |t| t.integer :foo_id, :primary_key => true end # Produces: # CREATE TABLE "foo" ( # "foo_id" integer, # PRIMARY KEY ("foo_id") # ); create_table('foo', :id => false) do |t| t.integer :foo_id, :primary_key => { :tablespace => 'fubar', :index_parameters => 'FILLFACTOR=10' } end # Produces: # CREATE TABLE "foo" ( # "foo_id" integer, # PRIMARY KEY ("foo_id") WITH (FILLFACTOR=10) USING INDEX TABLESPACE "fubar" # );
Table Definition¶ ↑
PRIMARY KEY constraints can also be applied to the table directly rather than on a column definition.
Examples¶ ↑
The following examples produces the same results as above:
create_table('foo', :id => false) do |t| t.integer :foo_id t.primary_key_constraint :foo_id end create_table('foo', :id => false) do |t| t.integer :foo_id t.primary_key_constraint :foo_id, { :tablespace => 'fubar', :index_parameters => 'FILLFACTOR=10' } end
Table Manipulation¶ ↑
You can also create new PRIMARY KEY constraints outside of a table definition using PostgreSQLAdapter#add_primary_key
or PostgreSQLAdapter#add_primary_key_constraint
.
Examples¶ ↑
add_primary_key(:foo, :bar_id) add_primary_key(:foo, [ :bar_id, :baz_id ]) add_primary_key(:foo, :bar_id, :name => 'foo_pk') add_primary_key(:foo, :bar_id, :tablespace => 'fubar', :index_parameters => 'FILLFACTOR=10') # Produces: # ALTER TABLE "foo" ADD PRIMARY KEY ("bar_id"); # ALTER TABLE "foo" ADD PRIMARY KEY ("bar_id", "baz_id"); # ALTER TABLE "foo" ADD CONSTRAINT "foo_pk" PRIMARY KEY ("bar_id"); # ALTER TABLE "foo" ADD PRIMARY KEY ("bar_id") WITH (FILLFACTOR=10) USING INDEX TABLESPACE "fubar";
Options for PRIMARY KEY Constraints¶ ↑
-
:name
- specifies a name for the constraint. -
:storage_parameters
- PostgreSQL allows you to add a couple of additional parameters to indexes to govern disk usage and such. This option is a simple String or a Hash that lets you insert these options as necessary. See the PostgreSQL documentation on index storage parameters for details.:index_parameters
can also be used. -
:tablespace
- allows you to specify a tablespace for the index being created. See the PostgreSQL documentation on tablespaces for details.
Dropping PRIMARY KEY Constraints¶ ↑
Like all PostgreSQL constraints, you can use PostgreSQLAdapter#drop_constraint
to remove a constraint from a table.