#100DaysOfCode in Python Transcripts
Chapter: Days 91-93: Database access with SQLAlchemy
Lecture: Demo: Defining database classes
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Now, the first thing we need to do
0:01
to use SQLAlchemy is to create some classes
0:04
that map to our database.
0:06
Now, these are classes,
0:07
theoretically, we could read and write them to the database
0:10
but there's a specific way in SQLAlchemy.
0:12
So, there's basically two steps that we have to follow.
0:15
The first one is to declare, create this base class
0:18
that SQLAlchemy knows about.
0:20
So, we're going to declare a specific base class
0:23
that SQLAlchemy knows about
0:25
and then everything that derives from it
0:26
is automatically going to be related to a database table.
0:31
And SQLAlchemy, by way of this derived aspects,
0:35
will learn about those tables and those classes.
0:38
So, the first thing that we're going to do is
0:40
going to look really, really lightweight.
0:42
It's going to look like, why did you
0:43
create a separate file for this,
0:45
but if you're going to do this nice partitioning
0:47
or have, one move class in one file,
0:49
one player class in it's own file,
0:51
one role class in it's own file, and so on,
0:53
it makes sense to go ahead and make one more,
0:55
albeit, super small class,
0:57
we're going to call this model base, like so.
1:00
And we're going to start just by importing SQLAlchemy.
1:04
Now, this is not going to go so well
1:05
because we don't have SQLAlchemy installed.
1:07
So, let's go over here
1:09
and actually make a requirements.txt file,
1:13
and put sqlalchemy.
1:15
Now, this is not set up.
1:16
Notice over here we have our virtual environment
1:18
if we do a pip list, there's only those few things here.
1:22
So, let's do a pip install --upgrade setuptools
1:26
don't know why this is so old
1:27
but it's like 10 versions out of date.
1:29
So, let's get it out of the way.
1:32
Now, we want, go ahead and let PyCharm install SQLAlchemy,
1:35
and it's all good.
1:37
So, go back to our model base here, this is good.
1:39
And what we actually want, is we're going to say
1:41
from sqlalchemy.ext.declarative
1:46
we're going to import declarative_base.
1:48
Now, this is a function, which when called
1:51
will create a class not an object, a class.
1:54
It's a little bit funky but here's how it works.
1:56
Instead of say it defining a class like this,
2:01
and you put some kind of base class and details,
2:04
we're going to let this function do it.
2:06
And we do it like this.
2:08
That's all there's to it, this whole file is now finished.
2:11
But anything that needs to become an entity
2:14
that's stored in our database, it derives from this.
2:17
So, now we can come over here and say,
2:18
hey role, you want to map to a database table?
2:21
We just import this,
2:24
like so,
2:25
and we're good.
2:28
Do the same for player.
2:32
And the move.
2:35
Now, PyCharm can go a little crazy here
2:37
and say oh, you need to add this to your requirements.
2:39
No, no, I don't.
2:41
This thing right here, that is their requirements.
2:44
You can come over here and put this little statement
2:46
to say, you know,
2:47
this is not actually an external thing, calm down.
2:50
Okay, so we're halfway there.
2:53
Step one is to derive from this model base,
2:55
on all the things we're going to map to the database.
2:58
Step two is going to be to define the columns.