Python for .NET Developers Transcripts
Chapter: Database access and ORMs in Python
Lecture: From in-memory classes to SQLAlchmey entities

Login or purchase this course to watch this video and the rest of the course contents.
0:00 It's time to get started with SQLAlchemy and Python's ORM version of our website. So, the first thing that I think is, makes sense
0:09 is to take this class and convert it from a standard class over into a specific SQLALchemy entity.
0:17 Right, right now all we're doing over in our services is we're just creating one. This is like a standard Python object. Nothing special about it.
0:25 So, how do we convert this to a SQLAlchemy class? Well, first thing we have to use a slightly different way of defining fields.
0:32 We are going to define static fields. And the way that you do that over here is you would just say things like name, lets just say equals empty string.
0:42 Now, if we do that, we still have self.name. This is a bit of difference between Python and C#. In C#, you either have a field which is static
0:55 or it's an instance one. In Python, the instance ones are just instance things. But, the static ones have a static copy
1:03 and then also, when you create instances they get their own copy. It's a little bit weird, but that's how it works.
1:10 So, what we are going to do is, we're not going to define these as instance versions. We are going to put them up here.
1:14 Like so. Alright, with that gone, we don't need or with that moved, we don't need this anymore. But let's go ahead and be super specific
1:22 that these are still strings. The way they're set up now yeah everything's going to know it's a string or float but you know what?
1:28 In a moment, we're all going to change things so they're not. Now, the next thing that we need to do is we need to create a bass class, so
1:38 like this. We need to derive from it. Like so. Now, that would be totally normal looking if this is how we're defining the class.
1:47 But, SQLAlchemy's ORM will, at runtime, create a type which is the bass class, but then the way SQLAlchemy knows what classes and tables to create is
1:58 it goes and says well, what objects derive from my bass class? Those are the ones apparently I'm in control of. So, we can't create the class this way
2:05 we're going to do something a little bit differently. We're going to go over here and say import SQLAlchemy.
2:13 Apparently we don't have that in our project, right? So let's go through the process to put that there. And then we'll go down here and pip install.
2:26 Okay, great! So we got that installed. We're going to go to sqlalchemy.ext.declarative. So, we're going to import that
2:32 and then here, this is the funky runtime bit. Say declarative_base. Just like that. And this is going to create a new type
2:39 just as if it was the way I had typed before. But, it's a special runtime class that we can derive from. Okay, so now this is what we want to do.
2:49 We got to go over here and change these into columns. Though we're going to go over here and say import sqlalchemy as sa
2:56 we use a little alias here so we can type less. So we are going to say this is a column in the database. And it is an integer. It's a primary key
3:10 and it's an auto-incrementing primary key for us. Actually, sorry this is a, We're going to need this.
3:17 Now let's go to the name. Going to be an sa.Column(sa.String). And that's that. Now we'll put the same over here, for style. Image going to be a float.
3:30 Now, we might want to do queries against these things. Remember, we're doing a query for one style or the other.
3:36 So you would, like an index on style of course that'll make it much faster if you have a lot of data. So we just say index=True.
3:43 Then, when SQLAlchemy creates the table it's going to add an index there. Similarly, we're ordering by price that's faster with an index as well.
3:51 So we can go like this. Probably we won't ask by name. Unlikely, very unlikely we ask by image.
3:58 And I guess we could say show us the ones that have no image but probably not. But I think that this is what we need.
4:05 Now, if I run it like this it's going to create the table and the database called that. I don't want it to be called capital G singular Guitar.
4:14 I want it to be lower case guitars. So we can go over here and add another field. Table name, __tablename__ = 'guitars'.
4:23 And this is what's actually going to be created in the database. Okay, this is it. This is how we define entities.
4:29 If we wanted, like, something else we'd have a class user, which is a SqlAlchemyBase. And so on.
4:38 If we're going to just leave all of these entities and classes in one giant file, we could do it, then this is fine.
4:45 We'll just put our SqlAlchemyBase at the top and we just roll with it. But, I hate that style. I want a class in its own file.
4:52 And, in order to make that work well in Python what we need to do is go over here and have another file
4:59 sqlalchemybase like this. This is going to look silly, but here we go. We're going to take this and put it there.
5:05 And we're going to take that and put it there. This is the entire implementation of the SqlAlchemyBase. And so, why am I doing this?
5:12 In Python, you can't have circular import dependencies. Okay, so like, if I put it here I need the thing that creates the guitar.
5:21 To, like, there's a few situations where this might have to be imported in multiple places and so on.
5:27 So we just need to import that up at the top, like so. There we go. Now, our guitar is ready to be saved into the database
5:35 Used for queries and actually generate the table that it represents with indexes and primary keys and all that.


Talk Python's Mastodon Michael Kennedy's Mastodon