https://github.com/hamuyuuki/activerecord-creating_foreign_keys
Define FOREIGN KEY Constraints in a CREATE TABLE Statement
https://github.com/hamuyuuki/activerecord-creating_foreign_keys
foreign-keys gem mysql rails ruby
Last synced: about 1 year ago
JSON representation
Define FOREIGN KEY Constraints in a CREATE TABLE Statement
- Host: GitHub
- URL: https://github.com/hamuyuuki/activerecord-creating_foreign_keys
- Owner: hamuyuuki
- License: mit
- Created: 2020-10-31T23:57:22.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-11-13T01:07:17.000Z (over 5 years ago)
- Last Synced: 2025-04-13T20:14:10.776Z (about 1 year ago)
- Topics: foreign-keys, gem, mysql, rails, ruby
- Language: Ruby
- Homepage: https://rubygems.org/gems/activerecord-creating_foreign_keys
- Size: 65.4 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
[](https://badge.fury.io/rb/activerecord-creating_foreign_keys)
[](https://travis-ci.com/hamuyuuki/activerecord-creating_foreign_keys)
[](https://codeclimate.com/github/hamuyuuki/activerecord-creating_foreign_keys/maintainability)
[](https://codeclimate.com/github/hamuyuuki/activerecord-creating_foreign_keys/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](https://guides.rubyonrails.org/v4.2/4_2_release_notes.html#foreign-key-support). And Rails 4.2.1 [supports adding a `:foreign_key` option to `references`](https://github.com/rails/rails/blob/4-2-stable/activerecord/CHANGELOG.md#rails-421-march-19-2015).
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](https://github.com/rails/rails/pull/20009/files). So `activerecord-creating_foreign_keys` backports that into Rails 4.2.
## Getting Started
Install `activerecord-creating_foreign_keys` at the command prompt:
```sh
gem install activerecord-creating_foreign_keys
```
Or add `activerecord-creating_foreign_keys` to your Gemfile:
```ruby
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**
```sql
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**
```sql
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](http://contributor-covenant.org) code of conduct.
## License
`activerecord-creating_foreign_keys` is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).