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 using SQLAlchemy. We have our session_factory and we're going to use it
0:06 to create a unit of work in our session. You saw we caught a really cool method called Create Session in our session context and all that
0:13 but we're just doing it from, like, bare you know, SQLAlchemy types here. We want to create a session want to create the object we want to insert
0:21 in this case, a guitar, and we're going to set some properties like the name is Ax Black, the price is 499
0:28 the image is this image, the style is electric. So we're going to set all the properties but notice we're also not setting the id
0:36 because that's auto-incrementing. When we save it, it's going to just go get set in the database so we don't have to set that
0:41 but all the other properties we do want set, set them here. Then we just go to the session and say Add, give it the object, and it's
0:47 nothing has happened yet on the database until we say, session.commit it goes to the database within a transaction
0:54 that inserts this record. Super easy, right?


Talk Python's Mastodon Michael Kennedy's Mastodon