Module: SchemaPlus::ForeignKeys::ActiveRecord::ConnectionAdapters::TableDefinition
- Defined in:
- lib/schema_plus/foreign_keys/active_record/connection_adapters/table_definition.rb
Overview
SchemaPlus::ForeignKeys adds several methods to TableDefinition, allowing indexes and foreign key constraints to be defined within a create_table
block of a migration, allowing for better encapsulation and more DRY definitions.
For example, without SchemaPlus::ForeignKeys you might define a table like this:
create_table :widgets do |t|
t.string :name
end
add_index :widgets, :name
But with SchemaPlus::ForeignKeys, the index can be defined within the create_table block, so you don’t need to repeat the table name:
create_table :widgets do |t|
t.string :name
t.index :name
end
Even more DRY, you can define the index as part of the column definition, via:
create_table :widgets do |t|
t.string :name, index: true
end
For details about the :index option (including unique and multi-column indexes), see the documentation for Migration::ClassMethods#add_column
SchemaPlus::ForeignKeys also supports creation of foreign key constraints analogously, using Migration::ClassMethods#add_foreign_key or TableDefinition#foreign_key or as part of the column definition, for example:
create_table :posts do |t| # not DRY
t.references :author
end
add_foreign_key :posts, :author_id, references: :authors
create_table :posts do |t| # DRYer
t.references :author
t.foreign_key :author_id, references: :authors
end
create_table :posts do |t| # Dryest
t.references :author, foreign_key: true
end
NOTE: In the standard configuration, SchemaPlus::ForeignKeys automatically creates foreign key constraints for columns whose names end in _id
. So the above examples are redundant, unless automatic creation was disabled at initialization in the global Config.
SchemaPlus::ForeignKeys likewise by default automatically creates foreign key constraints for columns defined via t.references
. However, SchemaPlus::ForeignKeys does not create foreign key constraints if the :polymorphic
option is true
Finally, the configuration for foreign keys can be overriden on a per-table basis by passing Config options to Migration::ClassMethods#create_table, such as
create_table :students, foreign_keys: {auto_create: false} do
t.references :student
end
Instance Attribute Summary collapse
Instance Attribute Details
#schema_plus_foreign_keys_config ⇒ Object
:nodoc:
70 71 72 |
# File 'lib/schema_plus/foreign_keys/active_record/connection_adapters/table_definition.rb', line 70 def schema_plus_foreign_keys_config @schema_plus_foreign_keys_config end |