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