This workshop will be retired on May 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
What to do about a MigrationPendingError.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Rails uses several components to
take a request from the browser and
0:01
return a response.
0:04
If any of these components is missing or
configured incorrectly,
0:05
your app won't work.
0:08
We're going to look at a few situations
where components are missing and
0:10
show you how to fix them.
0:13
To handle requests,
Rails needs several things.
0:15
First, an up-to-date database table.
0:17
If the table doesn't exist or
the data is in the wrong columns,
0:19
Rails won't retrieve
your model successfully.
0:23
Second, a route telling
it which controller and
0:26
which action should handle requests for
a given URL.
0:28
And third, a controller action
to actually handle the request.
0:32
Let's look at a situation where
the database is out of sync with our app
0:37
code and see how to fix it.
0:40
Our vet app tracks pets, but
not the owners to which they belong.
0:42
Let's create an owner model now.
0:46
So we'll say rails generate model
with a name of Owner, capital O.
0:48
And we'll give it just one attribute,
a name which is of type string.
0:58
So rails just created an Owner
model here in app/models/owner.rb,
1:04
but we're a long way from being
able to view owners in a browser.
1:10
If we try to go to localhost
port 3000 /owners right now,
1:15
whoops, I forgot to start my server.
1:20
Let me do that real quick.
1:22
bin/rails server.
1:24
So if we try to go to the owners
path right now, we get an error.
1:29
It says there's a pending migration error.
1:34
Let's see if we can solve this problem and
get back on track.
1:37
We can start by looking at
the stack trace as we did before.
1:41
But if we do, we'll see that
the error is not in our app.
1:44
Instead, it's within
the Active Record library.
1:47
So in this case, we don't want to
try to open the file in our editor.
1:51
Changing code in Active Record or
Rails won't fix our current problem, but
1:55
could create many new ones.
1:59
So what do we do?
2:01
Let's start by looking at the helpful
error message Rails printed for us.
2:03
It says, migrations are pending.
2:06
To resolve this issue, run: bin/rails
db:migrate in RAILS_ENV=development.
2:09
Let's go to our terminal and
try running that command.
2:16
So I'll quit out of my Rails server and
2:24
paste the command we copied
from the browser here.
2:27
I should mention that commands run against
the development database by default.
2:32
So you can drop the RAILS_ENV=development
environment variable if you want to, but
2:35
it doesn't hurt to leave it on.
2:41
So we hit Enter to run the command,
Rails runs the migration.
2:44
And let's try running the server again.
2:47
Reload the page, and
2:52
now we get a different error, but at least
the pending migration error is gone.
2:54
So what happened?
2:58
When we generated our Owner model,
3:00
Rails also created a migration
file in the db/migrate folder.
3:02
Here it is.
3:08
It has instructions to update
the database to store our owner objects.
3:10
But we haven't run it yet.
3:14
So we have an Owner class in our Ruby
code, but no owners table in our database.
3:15
When your code doesn't match the structure
of your database, bad things can happen.
3:20
You can create objects, but
3:24
if no table exists to hold them,
you won't be able to save them.
3:26
If you try to set an object attribute but
no matching column exists in the database,
3:30
you'll get errors.
3:34
You always want your code and
your database to be in sync.
3:35
For this reason, Rails keeps track
of which migrations you've run and
3:39
which you haven't.
3:43
When it saw the create owners file
in your migration directory and
3:44
saw that it hadn't been run yet,
it raised an error.
3:47
Run the migration and your database and
code will be back in sync and
3:50
that error goes away.
3:54
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up