Full Web Apps with FastAPI Transcripts
Chapter: Appendix: Using SQLAlchemy
Lecture: Creating the basic insert script
0:00 In this chapter, we're going to look at actually using SQLAlchemy.
0:04 Previously we had modeled all of our data
0:06 but we didn't do anything with it.
0:08 We didn't do any insert queries, updates
0:10 none of that.
0:11 That's what we're going to do now.
0:12 And to get started
0:13 we're just going to jump right into writing some code.
0:15 And so I just want to point out
0:17 we are now in Chapter 10
0:18 Using SQLAlchemy, working on the final bits here.
0:21 So let's switch over to PyCharm
0:23 grab our new chapter and get going.
0:26 This is the code from before, just carrying on.
0:29 And what we're going to do is
0:31 we're going to actually have over here
0:33 a new directory called bin.
0:35 Now, this is just a convention that I use.
0:37 I've seen it in other places as well
0:39 and this bin folder comes along with our website
0:41 for little admin tasks and scripts that we need to run
0:44 and so on.
0:45 It's not directly involved in running the site
0:47 but more like maintaining the site.
0:49 So, for example
0:50 we're going to do some importing of data.
0:52 And to do so, we're just going to write some scripts here.
0:55 They don't actually run normally
0:57 but they're going to run here.
0:59 Let's go over and add a Python file called basic_inserts.py.
1:03 We're going to take two passes at showing how to insert data.
1:07 First, we're going to write some example data
1:09 just standard make-up stuff.
1:12 And then I'm going to show you
1:13 I've actually got a hold of the real PyPI data
1:17 for the top 100 packages using their API
1:20 and we're going to insert that.
1:21 Turned out that's super, super tricky.
1:24 There's lots of little nuances and typecasting
1:26 and all that kind of stuff we have to do
1:28 to make it work just right.
1:29 We're not going to do that first
1:30 we're going to do like a simple example
1:32 and then I'll show you the program
1:33 that'll actually generate our real database.
1:36 So, here it is.
1:37 We already have our database right here
1:41 and if we look at it
1:44 we'll see that we have our packages
1:46 and releases put together.
1:47 And, of course, there are the interesting ones.
1:49 Actually, I'll go over here and show you a little more.
1:53 Show you a visualization pop-up.
1:55 It's kind of a cool feature of PyCharm.
1:57 So we have our packages and this relationship
1:59 between the releases.
2:00 That's probably the most interesting part of our database.
2:03 We didn't actually set up save, like
2:06 the maintainers and what not here.
2:08 This should maybe have, like some relationships and so on
2:11 but we didn't set up all the relationships
2:13 for our data model, just the really important ones.
2:15 So, we're going to focus on just those two tables.