![]() ![]() ![]() Now let's create a title, and I'm going to say: ![]() The next one is going to have an ID of 2, and so on and so forth. So the first guide that gets created is automatically going to have an ID of 1. The other thing is that this ID will automatically increment, so every time that our API creates a new record in the database it is going to increment. So every guide is going to have a unique ID associated with it. The two key items that are primary key does for you is a primary key will make sure it's always unique. Now if you're not familiar with the way databases work, or you don't have a lot of experience with it, what a primary key does is it allows you to have some nice built-in functionality. It has a data type of integer and it's a primary key. You say that we want to add a new column to that object. This is a syntax that's SQL Alchemy uses where you use the database object that we created. We're never going to reference this except for database look-up, so we're never going to have to set this. What this means is this is going to be a built-in column. Id = db.Column(db.Integer, primary_key=True) I'm going to create an ID column and reference that database object again. Now we're going to add three database columns, so this is where we're saying this is how we want our database to look like. So we're saying that Guide, and this whole class, is going to inherit from that model. You may notice this db call right here is calling this object, the SQL Alchemy object that we created on line 10. So right here I'm going to say class Guide, this is going to inherit from (db.Model). It's going to have a title and it's going to have content. We're going to have an API that has guides, and the guide is going to have two columns. For right now what I think a good example would be to create a little learning management system. What we're working through are just the basic fundamentals of how flask works, and so we're not going to be able to use this in a production application. ![]() I know we haven't really talked about the type of application we're going to build out, mainly because you can build anything that you want in this particular project. I'm going to start off with this, and I'm going to call OS, our operating system library, and then say:īasedir = os.path.abspath(os.path.dirname(_file_))Īpp.config = 'sqlite:///' + os.path.join(basedir, 'app.sqlite') This is going to stand for the base directory of our application because if we don't know what the base directory is then flask isn't going to know where to save our SQLite table to. I'm going to start off by creating a variable here called basedir. In order to do that, if you remember back on line 4, we imported the OS library which stands for operating system. So we need to be able to save our database to our project. The next thing we're going to do is we're going to integrate with the file system. We're going to be building now our own API endpoint, so let's just remove that just so it doesn't get in the way. Now the first thing I'm going to do is, we don't need this basic app route or anything like that here anymore. That means we are going to define the table that we want to work with, and then we are going to allow that code to generate the table for us. That's what we do in this guide and we're going to create what is called a schema. Instead, we're going to be able to simply create a file that will function exactly like how a database would. It's what's called an embedded database, and that means that we're going to be able to not have to worry about setting up an entire development environment for a database like we would have to do with my SQL or Postgres. We're going to use what is called SQLite. We are not going to use mySQL or anything like Oracle. What we're going to do is we're going to programmatically create a database table, and we're going to use a pretty basic database. Simply look at this as a high-level type of understanding, and try to have that for building out what we're going to do here. Don't worry about understanding everything that we're going to talk about. What my recommendation would be to follow along and see what gets built out. Now in this guide, what we're going to do, and just be prepared, this one is going to be a little longer and it's also going to be a little bit complicated if you've never done it before. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |