Python-powered Chat Apps with Twilio and SendGrid Transcripts
Chapter: Using our database with the SQLAlchemy ORM
Lecture: Defining the ORM order
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Well, let's start writing some code. And now that we've moved on from Chapter 4 to 5,
0:05
I've made an exact copy of what we finished with with 4 and what we're going to start with for 5. Be sure to update your location in GitHub.
0:15
If you're following along with the code, there, also notice there's a Cloud City flow start. This is the JSON definition of our studio flow,
0:24
as it is at the start of this chapter. If I make changes to the flow, then I'll come back here, of course, an update that as, well,
0:32
fantastic. So in order to use SQLAlchemy, the primary thing we do is we define classes, and these classes map over to tables in the database,
0:40
and they also linked together through relationships. Maybe I will call those models, but because we already have models from pydantic.
0:47
Remember our data exchange format for our API's is here. We're going to create similar things.
0:51
But instead of being something that drives from a pydantic model and does that kind of thing, it's going to be a SQLalchemy model with similar things.
1:00
So just to keep them distinct, let's go over here and make a directory called DB. I'm gonna start by defining the top level classes,
1:09
and then we're going to make them SQLAlchemy compatible, I guess, if you will. So let's work with the order.
1:14
The two classes were going to work with our order and customers or users, who are users are going to have multiple orders,
1:21
potentially or none, I suppose. But we'll start with order. So the way it works is we're gonna define a
1:27
class and seems like order is a good name for it and then the fields of the class map over to the columns in the database.
1:36
So we're going to have an ID, which is going to be an integer. And let me just write this out in plain Python and then add on the
1:42
SQLAlchemy aspects to it. I have a created date, and that's gonna be a date, time, datetime like that. I like all of my records in the database to
1:56
have some kind of information about when they were created. But another thing that our order is going to have to do is it's going to
2:03
have to go through this workflow first we get the order and then we start working
2:07
on it. Then we fulfill the order and let the customer know your cake is actually done not just received. We're going to have a fulfilled date as well.
2:17
And we have information about the cake, like the size which is a string, remember, Small, medium, large as a flavor.
2:26
Now I'll use the British belly because remember, our API is implemented by has, British billionaire, so just keep it consistent.
2:32
We've topping also a string like Sprinkles. frosting like chocolate. Also a string. We have a price, which is how many gold coins we gotta pay.
2:43
This is a float. And then we're gonna have some information how this relates over to the user because stand alone here.
2:49
This is a great order, but who does it belong to? We're going to use a foreign key by specifying the id of the user.
2:56
There we have it. This is our order class and sort of bare data form before we turn it into a
3:02
SQLAlchemy classes are blank order class that we're going to store in the database. It's going to have an ID, created date,
3:08
fulfilled date, size and so on as the columns with the data types that we've shown here.