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.integer :author_id
end
add_foreign_key :posts, :author_id, :references => :authors

create_table :posts do |t|  # DRYer
   t.integer :author_id
   t.foreign_key :author_id, :references => :authors
end

create_table :posts do |t|  # Dryest
   t.integer :author_id, :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.integer :student_id
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#schema_plus_foreign_keys_configObject

:nodoc:



68
69
70
# File 'lib/schema_plus/foreign_keys/active_record/connection_adapters/table_definition.rb', line 68

def schema_plus_foreign_keys_config
  @schema_plus_foreign_keys_config
end

Instance Method Details

#foreign_key(*args) ⇒ Object

(column_names, to_table, primary_key=nil, options=nil)



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/schema_plus/foreign_keys/active_record/connection_adapters/table_definition.rb', line 88

def foreign_key(*args) # (column_names, to_table, primary_key=nil, options=nil)
  options = args.extract_options!
  case args.length
  when 1
    to_table = args[0]
    column_names = "#{to_table.to_s.singularize}_id"
  when 2
    column_names, to_table = args
  when 3
    ActiveSupport::Deprecation.warn "positional arg for foreign primary key is deprecated, use :primary_key option instead"
    column_names, to_table, primary_key = args
    options.merge!(:primary_key => primary_key)
  else
    raise ArgumentError, "wrong number of arguments (#{args.length}) for foreign_key(column_names, table_name, options)"
  end

  options.merge!(:column => column_names)
  options.reverse_merge!(:name => ForeignKeyDefinition.default_name(self.name, column_names))
  fk = ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(self.name, AbstractAdapter.proper_table_name(to_table), options)
  foreign_keys_for_table(fk.to_table) << fk
  self
end

#foreign_keysObject



71
72
73
# File 'lib/schema_plus/foreign_keys/active_record/connection_adapters/table_definition.rb', line 71

def foreign_keys
  @foreign_keys ||= []
end

#foreign_keys_for_tableObject



75
76
77
# File 'lib/schema_plus/foreign_keys/active_record/connection_adapters/table_definition.rb', line 75

def foreign_keys_for_table(*)
  foreign_keys
end