#100DaysOfCode in Python Transcripts
Chapter: Days 91-93: Database access with SQLAlchemy
Lecture: Your turn: Day 2

Login or purchase this course to watch this video and the rest of the course contents.
0:00 On Day 2, you're going to focus on building out
0:03 the database models and the database structure.
0:05 So, recall from the presentation, that what you need to do
0:08 is use some kind of base class that comes from
0:12 SQLAlchemy, and have all of your models derive from that.
0:15 So, here's the example we had for our move, in our game,
0:18 and it derived from model base, we controlled the table,
0:21 by putting table name in there.
0:23 This is optional, but I like to do it.
0:26 And then we just defined all the columns.
0:27 Has integers, or has date times or strings,
0:30 and then be sure to give it a primary key,
0:33 and auto-increment if that's an integer, nice and easy.
0:36 And then you're going to need to actually go and create
0:40 the database, using your base model there.
0:43 And create a session factory for use later.
0:46 Alright, so if you run this code, already,
0:48 you should actually have some kind of database file,
0:50 and whatever you call it here, you'll have down here.
0:54 And then, you can either look at it,
0:55 if you're using Pycharm Pro, in Pycharm Pro,
0:58 or you can use a DB Browser for SQLite.
1:01 Either way, this is going to get your database structure
1:04 all up and running.
1:05 Final warning, or final note here:
1:07 Beware, you cannot modify existing tables, so for example,
1:11 this move, if I decided I also wanted some other value here,
1:16 like the opponent's role, or you know, whatever,
1:19 if I want to change this, at all, once I run this bit here,
1:24 it's fixed, can't change it.
1:26 SQLAlchemy will just ignore it, and it probably will crash
1:29 when you try to work with it, who knows.
1:30 So, if you want to do that, you need to use what's called
1:32 migrations, or for this little example, the easiest way
1:35 would be to just delete the database file, and start over
1:38 and it will create it, new.
1:39 But in production, migrations, or some kind of
1:42 database script to do the change, is what's required.