Module: ActiveRecord::ConnectionAdapters::OracleEnhancedTable
- Defined in:
- lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#foreign_key(to_table, options = {}) ⇒ Object
Adds a new foreign key to the table.
-
#references_with_foreign_keys(*args) ⇒ Object
Adds a :foreign_key option to TableDefinition.references.
-
#remove_foreign_key(options = {}) ⇒ Object
Remove the given foreign key from the table.
Class Method Details
.included(base) ⇒ Object
:nodoc:
143 144 145 146 147 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 143 def self.included(base) #:nodoc: base.class_eval do alias_method_chain :references, :foreign_keys end end |
Instance Method Details
#foreign_key(to_table, options = {}) ⇒ Object
Adds a new foreign key to the table. to_table
can be a single Symbol, or an Array of Symbols. See SchemaStatements#add_foreign_key
Examples
Creating a simple foreign key
t.foreign_key(:people)
Defining the column
t.foreign_key(:people, :column => :sender_id)
Creating a named foreign key
t.foreign_key(:people, :column => :sender_id, :name => 'sender_foreign_key')
Defining the column of the to_table
.
t.foreign_key(:people, :column => :sender_id, :primary_key => :person_id)
161 162 163 164 165 166 167 168 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 161 def foreign_key(to_table, = {}) if @base.respond_to?(:supports_foreign_keys?) && @base.supports_foreign_keys? to_table = to_table.to_s.pluralize if ActiveRecord::Base.pluralize_table_names @base.add_foreign_key(@table_name, to_table, ) else raise ArgumentError, "this ActiveRecord adapter is not supporting foreign_key definition" end end |
#references_with_foreign_keys(*args) ⇒ Object
Adds a :foreign_key option to TableDefinition.references. If :foreign_key is true, a foreign key constraint is added to the table. You can also specify a hash, which is passed as foreign key options.
Examples
Add goat_id column and a foreign key to the goats table.
t.references(:goat, :foreign_key => true)
Add goat_id column and a cascading foreign key to the goats table.
t.references(:goat, :foreign_key => {:dependent => :delete})
Note: No foreign key is created if :polymorphic => true is used.
194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 194 def references_with_foreign_keys(*args) = args. polymorphic = [:polymorphic] = .delete(:foreign_key) references_without_foreign_keys(*(args << )) # references_without_foreign_keys adds {:type => :integer} args. if && !polymorphic = {} if == true args.each { |to_table| foreign_key(to_table, ) } end end |
#remove_foreign_key(options = {}) ⇒ Object
Remove the given foreign key from the table.
Examples
Remove the suppliers_company_id_fk in the suppliers table.
t.remove_foreign_key :companies
Remove the foreign key named accounts_branch_id_fk in the accounts table.
remove_foreign_key :column => :branch_id
Remove the foreign key named party_foreign_key in the accounts table.
remove_index :name => :party_foreign_key
179 180 181 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_schema_definitions.rb', line 179 def remove_foreign_key( = {}) @base.remove_foreign_key(@table_name, ) end |