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