RESTful and HTTP APIs in Pyramid Transcripts
Chapter: Adding a database backend with SQLAlchemy
Lecture: Getting started SQLAlchemy

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Now in order to use sqlalchemy, there's a couple of things we're going to need to get set up as foundational items
0:06 before we can model our classes, with sqlalchemy. The first one is to just indicate that hey we depend upon sqlalchemy now,
0:13 so it's going to be important for later, when we actually want to deploy this, right,
0:18 so I could click this button here, I didn't do it last time, I'll do it this time.
0:22 We wait a moment, great it looks like everything was installed successfully into our virtual environment, we could of course check if we say pip list,
0:29 now we should have sqlalchemy right there, apparently 119 is the latest version, ok so we have sqlalchemy here;
0:37 the next thing we need to do is we need to create a base class, now the way it works basically is for each database that you want to talk to
0:45 you created base class, and when you derive from that class, it tells sqlalchemy here is a type I'm supposed to manage
0:52 and then you can do things like create the tables based on all the types that derive from this type, things like that.
0:58 So the first thing we need to do is actually create this base type now it's really simple and it is not at all complicated,
1:03 but I am going to put it into its own file, just so that you know where to go to manage it, it's clear where this base class lives, things like that.
1:12 If we have say like an analytics database and a core database, we'd have two base classes, like a sqlalchemy core base
1:18 and a sqlalchemy analytics base, but, we are not that advanced here. Okay, so let's go and create a new file, I'll call this sqlalchemy base,
1:26 alright, so we're going to import sqlalchemy.ext.declarative, let's do it this way, let's say from that import declarative base,
1:41 so this is like a factory type thing, a function when called, will actually create a type dynamically, like I said,
1:49 there should be one type per database, that all of your models derive from, and really huge one
1:55 that I guess could make sense to like have multiple ones if they never intersect with each other, but I always just create one,
2:00 so we'll call this sqla l c hemy base, it's going to be this, right, so this is like a factory function, factory method that is going to create
2:07 a type then we can use that type to derive from. So like I said, there is not a lot going on here, but this is what we got to do to get started.


Talk Python's Mastodon Michael Kennedy's Mastodon