Variations on Foreign Key Migrations6:31 with Jay McGavren
There are many ways to add the foreign key column that's required by our has_many and belongs_to associations, and I want to take a moment to show you a couple more of them.
There are many ways to add the foreign key column that's required by our
belongs_to associations, and I want to take a moment to show you a couple more of them.
- If you want, you can use the
referencescolumn type in migrations instead.
bin/rails generate migration AddPostToComments post:references
- That will create a migration with a call to the
add_referencemethod instead of
add_referencetakes a symbol with a table name, and a symbol with the name of a model to add a foreign key for. It'll create a column whose name begins with that model name, and ends in
_id. And since it's always desirable to have an index on foreign key columns,
add_referencewill add an index as well. So
add_reference :comments, :postwill create a
post_idcolumn in the
commentstable just like
add_columndid, but it will also add an index on that column automatically.
foreign_key: trueargument will set up a foreign key constraint on databases that support it.
class AddPostToComments < ActiveRecord::Migration[5.0] def change add_reference :comments, :post, foreign_key: true end end
Without a foreign key constraint, we could create a comment with a
post_id field set to
999, even if there was no record in the
post table with an
999. With a foreign key constraint, the database would prevent such a record from even being saved. Foreign key constraints help keep bad data from sneaking into your database.
Note that the adapter for the SQLite database that Rails uses by default doesn't support foreign key constraints. Your migration will still work, and it's a good idea to get in the habit of adding the constraints in your migrations. But if you want the database to actually enforce the constraints, you'll need to switch to another database like MySQL or PostgreSQL.
If we know we're going to need an association when we're first creating a model, we can set the necessary columns up then, too.
- Let's re-generate the
Commentmodel. We can replace the two migrations with a single migration that creates the
commentstable and adds a
post_idcolumn along with the other columns:
rails g model Comment content:text name:string post:references.
- We can allow it to overwrite the existing model class file, as well as another file related to tests.
- Don't forget to run
- We should now be able to create comments associated with a post again.
There are many ways to add the foreign_key column that's required by our has_many and 0:00 belongs_to associations. 0:04 And I wanna take a moment to show you a couple more of them. 0:06 Let's undo our changes to the database by rolling back the latest migration. 0:09 We'll run bin/rails. 0:13 And we'll run the db:rollback task. 0:16 That'll go through the commands in the latest migration and undo them one by one. 0:20 Then let's get rid of the migration we created. 0:25 We're gonna do that with bin/rails destroy, 0:27 which you can consider to be the opposite of the generate command. 0:31 We're gonna destroy the migration named AddPostToComments. 0:35 That's the latest migration we created. 0:44 That'll go through, 0:47 find all the files that were created by the generator, and delete them. 0:48 Now let's redo our previous migration using the references column type instead. 0:52 So we're gonna run bin/rails generate. 0:56 We're gonna generate a migration. 1:02 And again, it'll be named AddPostToComments. 1:04 This time, instead of a type of integer, we're going to give it 1:09 a column name of post and a type of references. 1:14 That'll create a new migration file. 1:21 And the file will have a call to the add_reference method instead of 1:27 add_column. 1:31 add_reference takes a symbol with a table name and a symbol with the name of 1:32 a model to add a foreign_key for, in this case, the post model. 1:36 It'll create a column whose name begins with that model name and 1:41 ends in underscore ID. 1:45 And since it's always desirable to have an index on foreign_key columns, 1:47 add_reference will add an index as well. 1:50 So add_reference :comments, 1:53 :post will create a post_id column in the comments table, just like add_column did. 1:54 But it'll also add an index on that column automatically. 1:59 The foreign_key: true argument will set up a foreign_key constraint 2:03 on databases that support it. 2:07 Without a foreign_key constraint, we could create a comment with a post_id field set 2:10 to 999, even if there was no record in the post table with an ID of 999. 2:14 But with a foreign_key constraint, 2:21 the database would prevent such a record from even being saved. 2:23 Note that the adapter for the SQL-like database that Rails uses by default 2:27 doesn't support foreign_key constraints. 2:31 Your migration will still work. 2:34 And it's a good idea to get in the habit of adding the constraints in your 2:35 migrations. 2:38 But if you want the database to actually enforce the constraints, 2:40 you'll need to switch to another database like MySQL or PostgreSQL. 2:43 See the teacher's notes if you'd like more info on doing so. 2:47 Now that we have our new migration, let's try running it. 2:51 bin/rails db:migrate. 2:54 It'll add a post_id field back to our comments table again. 3:01 And if we launch a Rails console, we'll be able to add comments to our post again. 3:04 So if we were to take the first post and 3:11 create a comment on it with comments.create, 3:16 we'll give it content of HI. 3:21 And the commenter name is gonna be Jay. 3:27 And if we pretty print the list of the first post comments, 3:31 we can see the comment's been added. 3:37 If we know we're going to need an association when we're first 3:42 creating a model, we can set the necessary columns up then, too. 3:45 Let's start by getting our database back to the point it was at 3:49 before we created the comments model. 3:52 Since we've only set up the comments table on our development machine, 3:54 we can just undo the migrations that created it. 3:57 We can list out the migrations in our db/migrate directory with ls db/migrate. 4:00 You might need to use a slightly different command to list the directory contents if 4:08 you're on Windows. 4:12 We can see that the two most recent migrations are add_post_to_comments and 4:13 create_comments. 4:18 Let's roll back our migration that adds the post_id field to the comments table. 4:20 bin/rails db:rollback. 4:24 Then let's roll back the migration that created the comments table. 4:30 bin/rails db:rollback again. 4:33 Now let's get rid of those last two migrations. 4:36 bin/rails destroy, 4:39 The migration named AddPostToComments. 4:44 And then we'll also destroy the migration named CreateComments. 4:52 Now let's regenerate the comment model. 5:00 We can replace the two migrations with a single migration that creates the comments 5:02 table and adds a post_id column along with the other columns. 5:07 So we'll say bin/rails 5:10 generate model Comment. 5:14 We'll set up a content attribute of type text and 5:19 commenter name attribute of type string. 5:24 And post:references will set up the post_id field. 5:28 If it asks to overwrite any existing files, it should be okay to allow it. 5:35 We generated the migration, so don't forget to run bin/rails db:migrate. 5:39 And if we launch bin/rails console, 5:47 we should be able to create comments associated with post again. 5:52 So let's say post = Post.first, 5:55 post.comments.create. 6:01 And we'll give it content of Hi and a commenter name of Alena. 6:05 And if we run Post.first.comments, it should bring up our new comment. 6:15 Now you know several ways to setup a has_many association. 6:22 In the next few videos, we're going to take a look at some of the other 6:26 associations that Active Record supports 6:28
You need to sign up for Treehouse in order to download course files.Sign up