Module: Foreigner::ConnectionAdapters::AbstractAdapter
- Defined in:
- lib/foreigner/connection_adapters/abstract/schema_statements.rb
Instance Method Summary collapse
-
#add_foreign_key(from_table, to_table, options = {}) ⇒ Object
Adds a new foreign key to the
from_table
, referencing the primary key ofto_table
. - #create_table(table_name, *args, &block) ⇒ Object
-
#foreign_key_exists?(table_name, options) ⇒ Boolean
Checks to see if a foreign key exists on a table for a given constraint.
-
#foreign_keys(table_name) ⇒ Object
Return the foreign keys for the schema_dumper.
-
#remove_foreign_key(from_table, options) ⇒ Object
Remove the given foreign key from the table.
- #supports_foreign_keys? ⇒ Boolean
Instance Method Details
#add_foreign_key(from_table, to_table, options = {}) ⇒ Object
Adds a new foreign key to the from_table
, referencing the primary key of to_table
The foreign key will be named after the from and to tables unless you pass :name
as an option.
Examples
Creating a foreign key
add_foreign_key(:comments, :posts)
generates
ALTER TABLE `comments` ADD CONSTRAINT
`comments_post_id_fk` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)
Creating a named foreign key
add_foreign_key(:comments, :posts, name: 'comments_belongs_to_posts')
generates
ALTER TABLE `comments` ADD CONSTRAINT
`comments_belongs_to_posts` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)
Creating a cascading foreign_key on a custom column
add_foreign_key(:people, :people, column: 'best_friend_id', dependent: :nullify)
generates
ALTER TABLE `people` ADD CONSTRAINT
`people_best_friend_id_fk` FOREIGN KEY (`best_friend_id`) REFERENCES `people` (`id`)
ON DELETE SET NULL
Supported options
- :column
-
Specify the column name on the from_table that references the to_table. By default this is guessed to be the singular name of the to_table with “_id” suffixed. So a to_table of :posts will use “post_id” as the default
:column
. - :primary_key
-
Specify the column name on the to_table that is referenced by this foreign key. By default this is assumed to be “id”.
- :name
-
Specify the name of the foreign key constraint. This defaults to use from_table and foreign key column.
- :dependent
-
If set to
:delete
, the associated records in from_table are deleted when records in to_table table are deleted. If set to:nullify
, the foreign key column is set toNULL
. - :options
-
Any extra options you want appended to the foreign key definition.
83 84 |
# File 'lib/foreigner/connection_adapters/abstract/schema_statements.rb', line 83 def add_foreign_key(from_table, to_table, = {}) end |
#create_table(table_name, *args, &block) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/foreigner/connection_adapters/abstract/schema_statements.rb', line 12 def create_table(table_name, *args, &block) definition = nil super do |td| definition = td # This is my trick to get the definition block.call(td) unless block.nil? end definition.foreign_keys.each do |to_table, | .each do || add_foreign_key(table_name, to_table, ) end end end |
#foreign_key_exists?(table_name, options) ⇒ Boolean
Checks to see if a foreign key exists on a table for a given constraint.
# Check a foreign key exists
foreign_key_exists?(:suppliers, :companies)
# Check a foreign key with a custom name exists
foreign_key_exists?(:suppliers, name: "fk_company_id")
# Check a foreign key on a column
foreign_key_exists?(:suppliers, column: "company_id")
40 41 |
# File 'lib/foreigner/connection_adapters/abstract/schema_statements.rb', line 40 def foreign_key_exists?(table_name, ) end |
#foreign_keys(table_name) ⇒ Object
Return the foreign keys for the schema_dumper
99 100 101 |
# File 'lib/foreigner/connection_adapters/abstract/schema_statements.rb', line 99 def foreign_keys(table_name) [] end |
#remove_foreign_key(from_table, options) ⇒ Object
Remove the given foreign key from the table.
Examples
Remove the suppliers_company_id_fk in the suppliers table.
remove_foreign_key :suppliers, :companies
Remove the foreign key named accounts_branch_id_fk in the accounts table.
remove_foreign_key :accounts, column: :branch_id
Remove the foreign key named party_foreign_key in the accounts table.
remove_foreign_key :accounts, name: :party_foreign_key
95 96 |
# File 'lib/foreigner/connection_adapters/abstract/schema_statements.rb', line 95 def remove_foreign_key(from_table, ) end |
#supports_foreign_keys? ⇒ Boolean
25 26 27 |
# File 'lib/foreigner/connection_adapters/abstract/schema_statements.rb', line 25 def supports_foreign_keys? false end |