Rails Generate Migration Foreign Key
If the column names cannot be derived from the table names, you can use the:column and:primarykey options. Rails will generate a name for every foreign key starting with fkrails followed by 10 characters which are deterministically generated from the fromtable and column. There is a:name option to specify a different name if needed. In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations. Generating Migrations. To create a migration, use the make:migration Artisan command: php artisan make:migration createuserstable. The new migration will be placed in your database/migrations directory. Each migration file name contains a timestamp, which allows Laravel to determine the order of the migrations.
Immigrant gives Rails a foreign key migration generator so you caneffortlessly find and add missing keys. This is particularly helpfulwhen you decide to add keys to an established Rails app.
Bin/rails generate migration AddPostToComments post:references That will create a migration with a call to the addreference method instead of addcolumn. Addreference takes a symbol with a table name, and a symbol with the name of a model to add a foreign key for.
Installation
Add the following to your Gemfile:
If you're using a version of Rails prior to 4.2, you'll also need theForeigner gem.
Usage
This will create a migration named AddKeys which will have add_foreign_key
statements for any missing foreign keys. Immigrant infers missing ones byevaluating the associations in your models (e.g. belongs_to
, has_many
, etc.).Only missing keys will be added; existing ones will never be altered orremoved.
Rake Task
To help you remember to add keys in the future, there's a handy raketask you can add to your CI setup. Just run rake immigrant:check_keys
,and if anything is missing it will tell you about it and exit with anon-zero status.
Skipping associations
https://neodisakaiser.weebly.com/blog/free-full-bookworm-deluxe-download. Immigrant.ignore_keys
allows you to specify a list of keys that shouldbe ignored (both in the migration generator and the rake task). This isuseful if you have associations spanning databases.
Just create an config/initializers/immigrant. Chance of ever generating same key twice 10 letters. rb file with something likethe following: /openssl-generate-private-key-encrypted.html.
Considerations
Rails Generate Migration Add Column
If the data in your tables is bad, then the migration will fail to run(obviously). IOW, ensure you don't have orphaned records before you try toadd foreign keys.
Rails Guides Migrations
Known Issues
Immigrant currently only looks for foreign keys in ActiveRecord::Base
'sdatabase. So if a model is using a different database connection and it hasforeign keys, Immigrant will incorrectly include them again in the generatedmigration. Immigrant.ignore_keys
can be used to work around this.
License
Rails Add Migration
Copyright (c) 2012-2015 Jon Jensen, released under the MIT license