Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Using our database with the SQLAlchemy ORM
Lecture: Concept: SQLAlchemy model class

Login or purchase this course to watch this video and the rest of the course contents.
0:00 To work with our database. We did very little direct database operations.
0:04 What instead we did is we said we're going to use an
0:06 ORM, an object relational mapper named SQLAlchemy.
0:10 And over there we create classes and then SQLAlchemy will manage the tables and the
0:15 inserts and the queries and everything like that for us.
0:18 Do we get started by giving a short name to the SQLAlchemy Name Space and
0:22 We Create a Class which derived from SQLAchemy Base,
0:25 which we created elsewhere. Give it a table name so that,
0:29 SQLAlchemy knows where to store it in the database.
0:33 And then we start defining the columns as fields in this class.
0:36 So we define ID, which is an sa column of type sa string
0:40 It's a primary key, and it's auto incrementing.
0:43 So when we do an insert,
0:44 it automatically gets the correct and latest value.
0:47 We don't have to worry about that.
0:49 It's also really good to know when these things were created.
0:52 When did this user sign up?
0:54 When was their order placed? We're going to have a creative date,
0:57 which is a datetime column, but we gave it a default value of datetime.daytime.now
1:01 a function not the result of calling that function.
1:05 Remember, no parentheses there. And what happens is whenever you insert something
1:10 SQLAlchemy will automatically call that function and set the creative date right when you did this
1:14 you don't have to do either of these things.
1:16 They're automatic as you save the data.
1:19 We want to give the user a name like Michael.
1:22 I want to give them a phone number like WhatsApp :+1503,
1:26 Whatever it is. And because we may want to query by their
1:29 phone number. We're gonna put an index here,
1:32 Same thing for their email, but as a string and give it an index.
1:36 We also have orders and users and their related to each other.
1:40 So we created this relationship. There's a relationship over to the order class Over there
1:45 There's a user field which gets back populated,
1:47 and here we have a list of orders a one to many relationship from user to
1:52 their orders, right here. And this allows us to quickly given a user,
1:57 go find their orders or given an order,step back and find the user its associated with.