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