Module | Foreigner::ConnectionAdapters::AbstractAdapter |
In: |
lib/foreigner/connection_adapters/abstract/schema_statements.rb
|
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
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