Python for Entrepreneurs Transcripts
Chapter: Accessing databases from Python: SQLAlchemy ORM
Lecture: Demo: Part 2 Modeling albums and tracks

Login or purchase this course to watch this video and the rest of the course contents.
0:01 So of course, we want to model albums, and albums have tracks and so on.
0:05 So let's go over here and create a class, called album, like so
0:09 and in here we are going to create a class called Album.
0:13 Now, remember what I said, in order to map this to the database
0:18 and have it participate in SQLAlchemy's ORM behaviors,
0:21 this class has to share that common base class that we've created,
0:24 so we'll say something like this:
0:31 "from modelbase import SqlAlchemyBase"
0:34 and then this is going to be our base class here.
0:36 We are also going to need to work with SQLAlchemy.
0:40 Maybe as you get more used to this, we'll import
0:43 the things that we're just about to use here,
0:45 but I am going to leave the namespaces or the module names on here
0:49 so that you can tell explicitly where things are coming from.
0:52 So of course, we want to specify what this shape,
0:55 what this looks like in the database.
0:58 And let's suppose it's going to start up by having an id,
1:01 that would be reasonable, right, this is going to be like our primary key,
1:04 notice that we're creating these at the type level, the class level,
1:07 not the instance level and the dunder init,
1:10 because we are going to use album.id as part of our query syntax later on.
1:15 So, it's important to create it this way,
1:18 we'll go over here and what we want to do is create a column, right,
1:21 and we are going to have a bunch of columns we are also going to have
1:24 a name and so on. But these columns, they take a couple of parameters,
1:27 one of the things they need is a type, so we'll have an integer, like so,
1:32 and we want this to be a primary key,
1:34 we'll come back and work on that in a minute.
1:36 We want this to be a string, and so on,
1:39 let's suppose we are going to have a year
1:42 and I'll just go and flash this data model out.
1:45 So here we have a basic album, now, there is a lot of stuff
1:50 that is actually missing here like uniqueness, constraints,
1:53 like defining what the primary key is, just having it called id is not enough,
1:58 and so on, we are going to come back to that in just a moment,
2:02 but let's go ahead and also add a track class.
2:07 Again, we are going to do the same thing,
2:09 we are going to import SQLAlchemy,
2:11 we are going to import our SQLAlchemy base class that we created,
2:16 and we are going to create a class called Track
2:19 and it's going to derive from SqlAlchemyBase.
2:22 OK, we're off to get started, and then
2:25 we're just going to specify the columns here as well.
2:29 There, we have an id, a name, a length, an audio and video URL
2:33 as well as the order in which this track should appear within the album.
2:37 Now there is a couple of other things that we should come back to and we will,
2:40 like relationships, this track should be tied to a particular album,
2:44 so there should be a many-to-one relationship from tracks to albums.
2:49 Now one final thing before we go on to working with these is...
2:53 I like to be very explicit about how this class maps to the database,
2:59 and so we can come over here, we can set a __tablename__,
3:02 to something like this, so I am going to call it Album
3:07 and PyCharm thinks this is misspelled, it's not, right,
3:11 so here I am going to make it Album,
3:13 you could make it something different than like a type name,
3:15 so it could be like albums, like this, plural lower case,
3:18 but there are times, especially when you are working with the relationships,
3:21 it's unclear whether you are talking about the thing in the database
3:24 or the thing in the model, and so this makes it very clear
3:28 if the name of table is the same as the name of your type.
3:31 But it doesn't have to be, right, if for some reason you are working with
3:34 like an existing database, you maybe don't control that.
3:38 OK, so now we've got these two classes
3:41 that are supposed to be mapped to the database,
3:43 they are incomplete, granted, they have no primary key
3:46 for example, that's really bad. But before we move on,
3:50 let's see how we can use SQLAlchemy to actually create the schema
3:53 and relationships within our database by just the information we put here.