Python for the .NET developer Transcripts
Chapter: Database access and ORMs in Python
Lecture: Concept: Inserting data

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's review quickly how to insert objects
0:02 using SQLAlchemy.
0:04 We have our session_factory and we're going to use it
0:05 to create a unit of work in our session.
0:08 You saw we caught a really cool method
0:10 called Create Session in our session context and all that
0:12 but we're just doing it from, like, bare
0:14 you know, SQLAlchemy types here.
0:16 We want to create a session
0:18 want to create the object we want to insert
0:20 in this case, a guitar, and we're going to set some properties
0:23 like the name is Ax Black, the price is 499
0:27 the image is this image, the style is electric.
0:31 So we're going to set all the properties
0:32 but notice we're also not setting the id
0:35 because that's auto-incrementing.
0:36 When we save it, it's going to just go get set in the database
0:39 so we don't have to set that
0:40 but all the other properties we do want set, set them here.
0:43 Then we just go to the session and say
0:44 Add, give it the object, and it's
0:46 nothing has happened yet on the database
0:49 until we say, session.commit
0:50 it goes to the database within a transaction
0:53 that inserts this record. Super easy, right?