#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 to use SQLAlchemy is to create some classes that map to our database. Now, these are classes,
0:08
theoretically, we could read and write them to the database but there's a specific way in SQLAlchemy.
0:13
So, there's basically two steps that we have to follow. The first one is to declare, create this base class that SQLAlchemy knows about.
0:21
So, we're going to declare a specific base class that SQLAlchemy knows about and then everything that derives from it
0:27
is automatically going to be related to a database table. And SQLAlchemy, by way of this derived aspects,
0:36
will learn about those tables and those classes. So, the first thing that we're going to do is going to look really, really lightweight.
0:43
It's going to look like, why did you create a separate file for this, but if you're going to do this nice partitioning
0:48
or have, one move class in one file, one player class in it's own file, one role class in it's own file, and so on,
0:54
it makes sense to go ahead and make one more, albeit, super small class, we're going to call this model base, like so.
1:01
And we're going to start just by importing SQLAlchemy. Now, this is not going to go so well because we don't have SQLAlchemy installed.
1:08
So, let's go over here and actually make a requirements.txt file, and put sqlalchemy. Now, this is not set up.
1:17
Notice over here we have our virtual environment if we do a pip list, there's only those few things here.
1:23
So, let's do a pip install --upgrade setuptools don't know why this is so old but it's like 10 versions out of date. So, let's get it out of the way.
1:33
Now, we want, go ahead and let PyCharm install SQLAlchemy, and it's all good. So, go back to our model base here, this is good.
1:40
And what we actually want, is we're going to say from sqlalchemy.ext.declarative we're going to import declarative_base.
1:49
Now, this is a function, which when called will create a class not an object, a class. It's a little bit funky but here's how it works.
1:57
Instead of say it defining a class like this, and you put some kind of base class and details, we're going to let this function do it.
2:07
And we do it like this. That's all there's to it, this whole file is now finished. But anything that needs to become an entity
2:15
that's stored in our database, it derives from this. So, now we can come over here and say, hey role, you want to map to a database table?
2:22
We just import this, like so, and we're good. Do the same for player. And the move. Now, PyCharm can go a little crazy here
2:38
and say oh, you need to add this to your requirements. No, no, I don't. This thing right here, that is their requirements.
2:45
You can come over here and put this little statement to say, you know, this is not actually an external thing, calm down. Okay, so we're halfway there.
2:54
Step one is to derive from this model base, on all the things we're going to map to the database. Step two is going to be to define the columns.