Full Web Apps with FastAPI Transcripts
Chapter: Appendix: Using SQLAlchemy
Lecture: Saving the package to the db
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Classes model records in our database so all we have to do is go and save this back into the database and that's where this unit of work
0:09
in the factory from db_session, comes from. So what we'll do is we'll say session equals db_session.factory. So, this is pretty cool.
0:19
If we come over here and say db_session. notice all the cool things you can do? No, no you don't. There's none. If I actually set the type here.
0:29
I'm not going to leave it like this but, if I were to import sqlalchemy.orm like this, and set it to a session all of a sudden, we have, oh look
0:41
autoflush, and commit, and bind and rollback, and all the database things and add, and so on. Let's go ahead and get this set up real quick
0:50
and then I'll show you what we'd actually do. We'll make this a little bit nicer. There's some stuff down the road.
0:56
We're going to need to make this better anyway. All right, so, we're going to do some work and then, the way this unit of work works
1:02
is you say, I'm going to make a bunch of changes in this area create the session, make a bunch of changes.
1:08
No interaction with the database will have happened until line 26, when we call commit or, if we had entered some kind of query
1:14
but we're not querying, we're only inserting. Let's do this. Let's come over here, and we'll say session.add. That's it.
1:24
If we run this, it's going to insert it right away but let's add a couple of releases. So we'll say, r., r is a release. I'm going to import that.
1:35
Let's go over to releases and see what we got to set. id auto_increment in, we don't need to set. Those, we should definitely set.
1:43
created_date is its own thing. So, let's just do those three and maybe the size. So, we'll say r.majorversion equals... Now, we're just going to assume
1:59
that they know how to type an integer and, if they don't, well, it's going to crash, but it's OK. It's just a little example.
2:06
Minor, and build. And, last one, will be size in bytes. OK. Looks pretty good. Now, how do we let's do a quick print.
2:26
Release one, something like that and let's do it again for release two. So there's actually more than one release per package, OK?
2:33
How do we associate this release with this package? There's a lot of ways. We could add each release separately. So we could say session.add each time
2:43
and this, and then commit it as long as we had set r.package_id = p.id. Now actually, in this case, this would be fine
2:54
but normally, windows are auto-incrementing IDs. You need to first save the package and then you can set it, and it gets real tricky, right?
3:01
Like, it's not super simple to do this but, what is super simple is to work with this relationship over on packages. So, let's go to here, package.
3:10
Remember this, releases? It's a relationship but it kind of acts like a list. Let's just try that. What happens if I say, p.releases.append
3:21
as if it were a list. You know that's going to work. It is, it's awesome. OK, so we're going to append those
3:28
and we're never going to tell the session to add it but it's going to traverse the relationships and go, I see you have other records
3:34
to insert into other tables, and relationships to set up.