Full Web Apps with FastAPI Transcripts
Chapter: Appendix: Using SQLAlchemy
Lecture: Running the inserts

Login or purchase this course to watch this video and the rest of the course contents.
0:00 I think we're ready to try this. Let's go ahead and run this and see what happens. Off it goes, package name let's go with SQLAlchemy to start.
0:11 Summary is ORM4 for Python. Author is Mike Bayer. License, let's just guess, it's MIT. The first version is 123. And it's that many bytes.
0:28 The second one is going to be 2.0.0 and it's a little bit bigger, like so. So that inserted the first one.
0:36 Let's do one more and say Flask, Microframework for Python. Let's go with Armand and David, right? Armand, originally, David Lord these days.
0:49 Let's just say this is BSD. I have no idea what it is. And it's 1.0.0 and 1.0.02. There we go, that one can be bigger than one byte.
1:01 All right, I think we're good. Let's go ahead and stop this. Notice, there were no crashes. That's pretty killer already.
1:07 That's a good chance that something worked But let's go look in the database. So if I go over to Packages and I say
1:13 Jump to Console and say, select * From Packages. Moment of truth. Tada! There they are, how awesome? We didn't set some of the values
1:25 but we did set many of them. You can see everything we typed in there. Pretty awesome, isn't it? What about releases?
1:34 Run those, look at that. There they are! And you can see what package they come from SQLAlchemy or Flask.
1:41 That's really cool and that's actually the relationship. So I could go over here and, say, where package ID equals SQLAlchemy? Is it 1?
1:57 I don't think it's what it equals. Here we go. So, we can go do the query for that and this is when we actually go back and do queries with SQLAlchemy
2:04 and we touch that releases folder. It's going to do, basically, this query but it's also going to add an order by major version descending.
2:13 And then minor and then whatever but this should be enough. There we go. So we'll get them back exactly in the order you would want them.
2:21 All right, so this is how we insert data. Super, super simple, right? We go and just treat these more or less like objects.
2:26 We create them, we set their properties we click them together through their relationships and we add them to the session, create a session and add it.


Talk Python's Mastodon Michael Kennedy's Mastodon