Gem Version Build Status Maintainability Test Coverage

activerecord-creating_foreign_keys

activerecord-creating_foreign_keys defines FOREIGN KEY Constraints in a CREATE TABLE Statement.

Rails 4.2 supports adding and removing foreign keys. And Rails 4.2.1 supports adding a :foreign_key option to references. But it defines FOREIGN KEY Constraints in a ALTER TABLE Statement as an additional DDL when you define a :foreign_key option to references.

Rails 5 supports defining FOREIGN KEY Constraints in a CREATE TABLE Statement. So activerecord-creating_foreign_keys backports that into Rails 4.2.

Getting Started

Install activerecord-creating_foreign_keys at the command prompt:

gem install activerecord-creating_foreign_keys

Or add activerecord-creating_foreign_keys to your Gemfile:

gem "activerecord-creating_foreign_keys"

How to use

You don't need to do anything after installing activerecord-creating_foreign_keys.

You can know Before and After if articles is created.

create_table :articles do |t|
  t.references :author, foreign_key: true
end

Before

CREATE TABLE `articles` (`id` int(11) auto_increment PRIMARY KEY, `author_id` int(11));
ALTER TABLE `articles` ADD CONSTRAINT `fk_rails_e74ce85cbc` FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`);

After

CREATE TABLE `articles` (`id` int(11) auto_increment PRIMARY KEY, `author_id` int(11), CONSTRAINT `fk_rails_e74ce85cbc` FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`));

Limitation

At this time, only the mysql2 adapter support this function.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/hamuyuuki/activerecord-creating_foreign_keys. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

activerecord-creating_foreign_keys is available as open source under the terms of the MIT License.