#100DaysOfWeb in Python Transcripts
Chapter: Days 61-64: Database migrations
Lecture: Seeing the problem in action

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So let's get started with Alembic.
0:01 And I don't actually have any code to get started with.
0:04 So what we're going to do
0:05 is we're going to take the last bit of code that we wrote
0:08 from the Pyramid chapter.
0:10 Remember, we did SQLAlchemy and then we did Pyramid
0:12 and our Pyramid app had our database stuff in there.
0:15 Clear this bill tracker thing.
0:16 So I'm going to copy this over.
0:18 Maybe I'll copy that part over for you
0:20 so you can just see how to
0:21 how I got totally started from scratch.
0:23 So we need to do just two quick things
0:25 to make sure that this is a fresh copy
0:27 and we didn't break it by moving
0:29 basically our virtual environment and our installed stuff.
0:32 So we're going to go to this folder.
0:34 Going to set it up so everything will runs well.
0:36 Might as well update that.
0:38 So when I go over here, and we're going to
0:40 run our venv command, which will
0:41 create a virtual environment.
0:42 You're going to see the command at the top
0:44 it's creating the virtual environment, updating it
0:46 and activating it.
0:47 And then, down here we need to run our setup
0:50 in development mode.
0:52 Okay, everything should be in place.
0:55 Should build a runner app. Looks like we can.
0:57 Alright, so let's get out of the terminal
0:59 and go over to PyCharm here and run it.
1:01 Super, we have our app here
1:03 and we should build a run it also in PyCharm.
1:06 See, it's running our migration one and does it work?
1:09 Course it does, it works like a charm.
1:11 So it looks like everything is working great.
1:14 First thing we want to do is like
1:15 let's just observe that what I showed you
1:17 in the slides there, that's actually a problem.
1:20 Let's go over to our models
1:21 and let's say we're just going to change
1:22 something super simple about the bill.
1:24 Like we've had paid, we've had the total
1:27 we had what's created, let's do this.
1:29 Let's have one more column here
1:31 called last_payment.
1:34 Maybe we want to track this.
1:35 Like, show me all the things I haven't paid in 30 days
1:38 or something to that effect.
1:39 I have an index, but let's not have a default value.
1:43 So I guess we should set null ability to true.
1:47 Alright, seems like that should work.
1:48 Let's run our code again. Boom, it's crashed.
1:51 There's the dreaded operational error I just told you about.
1:54 Bills.last_payment. Doesn't exist.
1:57 Right, so our model believes there's a last payment
2:00 'cause it's right here, but our database doesn't have it.
2:03 And some databases are more flexible
2:05 but relational data bases, they're generally
2:07 very, very strict about their schema and their relationship.
2:11 And remember, SQLAlchemy won't update existing tables.
2:15 It will create new ones but it will not update existing ones.
2:17 So we're stuck, we have to fix our database to make this work.
2:20 Alright, so we can't even get started.
2:22 Right, we can't even get our website to load
2:25 much less to get an error out of one of our web pages.
2:28 So that's where we are
2:29 and we're going to fix it with Alembic and database migrations.