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.
add_foreign_key(:comments, :posts)
generates
ALTER TABLE `comments` ADD CONSTRAINT `comments_post_id_fk` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)
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`)
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
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
.
Specify the column name on the to_table that is referenced by this foreign key. By default this is assumed to be "id".
Specify the name of the foreign key constraint. This defaults to use from_table and foreign key column.
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 to NULL
.
Any extra options you want appended to the foreign key definition.
# File lib/foreigner/connection_adapters/abstract/schema_statements.rb, line 56 def add_foreign_key(from_table, to_table, options = {}) end
Return the foreign keys for the schema_dumper
# File lib/foreigner/connection_adapters/abstract/schema_statements.rb, line 72 def foreign_keys(table_name) [] end
Remove the given foreign key from the table.
remove_foreign_key :suppliers, :companies
remove_foreign_key :accounts, column: :branch_id
remove_foreign_key :accounts, name: :party_foreign_key
# File lib/foreigner/connection_adapters/abstract/schema_statements.rb, line 68 def remove_foreign_key(from_table, options) end
# File lib/foreigner/connection_adapters/abstract/schema_statements.rb, line 12 def supports_foreign_keys? false end