RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Adding a database backend with SQLAlchemy
Lecture: Creating tables

Login or purchase this course to watch this video and the rest of the course contents.
0:01 So we've modeled our car, and I think we have that thing pretty much dialed in at this point, the next thing to do is
0:08 actually create the database so that we can start working. Now, relational databases need the schema builtin,
0:14 all that kind of stuff, and it turns out sqlalchemy is great for doing that so we can come over here and if we have access to the sqlalchemy base class
0:22 which if we import it of course, we will, it has this thing called metadata now it doesn't show up here but just roll with me on this
0:32 and the metadata you can call create all and provided the engine and that line right there will go look at the database
0:38 if it doesn't exist at all, like sqlite here, it will create the database and then it will look at the tables and create any that are missing,
0:44 it's important that it will not update existing tables so if I run this then I make a change to my car class
0:50 it's not going to work right, this create all will not refresh that schema but it will get you started, right;
0:58 afterwards you have to run migrations or scripts and things like this, or if there's data that was auto generated just delete it and generate it again.
1:05 So let's run this and because we have echo= true going on we should see some stuff happen,
1:11 look at all that, boom, boom, boom, we're doing all sorts of stuff going over here, creating index on year, index on last seen,
1:20 a bunch of stuffing you can see it's creating the table car that's what we told it, with all of these values,
1:25 varchar, integer, datetime, things like that, so let's look over here and see what we got notice, nothing here, click, now wait for it synchronize db,
1:36 ha, there it is, sqlite, how do we look at it, over here we have a database view, I'm going to drop it over here and it's not going to work,
1:43 by the way the database view only works on PyCharm Pro, the database features are not part of the community edition,
1:49 alright, so let me drop this over here, and it kind of gives you a squiggly and you open it up and things are not great,
1:56 so the reason is I've got to do one little bit of configuration to PyCharm, so let's go to properties and notice it thinks this is sqlite,
2:03 that's pretty solid of it, but it says the drivers to understand this to basically manage this are missing from PyCharm, so you want to install that.
2:12 Ok now we've got it downloaded let's hit test connection, just to be sure— success, all right cool, so if we go back over here and we hit refresh,
2:19 now if we look in there, we have the car and look at all the pieces it's got our indexes, it's got our column types,
2:26 our primary key all those sorts of things. So that is solid, but there's no data in it yet
2:33 so the next thing we have to do is insert some data into the database.


Talk Python's Mastodon Michael Kennedy's Mastodon