Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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 4 hours ago
JSON representation

Define FOREIGN KEY Constraints in a CREATE TABLE Statement

Awesome Lists containing this project

README

        

[![Gem Version](https://badge.fury.io/rb/activerecord-creating_foreign_keys.svg)](https://badge.fury.io/rb/activerecord-creating_foreign_keys)
[![Build Status](https://travis-ci.com/hamuyuuki/activerecord-creating_foreign_keys.svg?branch=master)](https://travis-ci.com/hamuyuuki/activerecord-creating_foreign_keys)
[![Maintainability](https://api.codeclimate.com/v1/badges/3cac3284bb083ea1f9cd/maintainability)](https://codeclimate.com/github/hamuyuuki/activerecord-creating_foreign_keys/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/3cac3284bb083ea1f9cd/test_coverage)](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).