Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Flask with SQLAlchemy Basics!
      
    
You have completed Flask with SQLAlchemy Basics!
Preview
    
      
  Connect your Flask website to a database using SQLAlchemy.
Flask-SQLAlchemy update
Be sure that your Dunder Main looks like this!
if __name__ == "__main__":
    with app.app_context():
        db.create_all()
    app.run(debug=True, port=8000, host='127.0.0.1')
datetime.now vs. datetime.now()
If you're getting a track modification error check this post out.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      [MUSIC]
                      0:00
                    
                    
                      Our website needs a database to
hold all of our pets.
                      0:04
                    
                    
                      We're going to use SQLAlchemy
to create our pets database.
                      0:08
                    
                    
                      Flask has a special SQLAlchemy
connection called Flask-SQLAlchemy.
                      0:12
                    
                    
                      Let's install it now,
                      0:19
                    
                    
                      pip install flask-SQLAlchemy.
                      0:23
                    
                    
                      There we go.
                      0:29
                    
                    
                      And then let's run pip freeze
> requirements.txt.
                      0:30
                    
                    
                      And if I pull up our requirements file,
                      0:34
                    
                    
                      you can see Flask-SQLAlchemy and
SQLAlchemy have been installed.
                      0:38
                    
                    
                      Perfect.
                      0:45
                    
                    
                      I'm also gonna run clear just
to clear up the console.
                      0:47
                    
                    
                      If you need a refresher,
                      0:51
                    
                    
                      I put a link in the teacher's notes to
our SQLAlchemy content, just in case.
                      0:52
                    
                    
                      Let's get started by looking at
the documentation for Flask-SQLAlchemy.
                      0:58
                    
                    
                      I did a quick Google of
Flask-SQLAlchemy and
                      1:03
                    
                    
                      pulled up their documentation and
clicked on Quickstart.
                      1:06
                    
                    
                      If I scroll down a bit,
                      1:12
                    
                    
                      it shows you exactly how we're
going to create our model.
                      1:14
                    
                    
                      At the top here, it's showing
us we are going to need Flask.
                      1:18
                    
                    
                      And we're gonna be creating our Flask
app inside of the models file instead
                      1:23
                    
                    
                      of our app file.
                      1:28
                    
                    
                      So we'll need to move it.
                      1:29
                    
                    
                      We'll also be importing SQLAlchemy,
then we'll be creating our database name.
                      1:31
                    
                    
                      So you can see here,
they called theirs test.db.
                      1:37
                    
                    
                      And then creating a db
variable using SQLAlchemy.
                      1:41
                    
                    
                      Our user model will take db.Model,
our columns will
                      1:47
                    
                    
                      be db.Column,
the data types will be db.Integer or
                      1:52
                    
                    
                      string, or whichever data type you need.
                      1:57
                    
                    
                      You're probably wondering,
where is session and
                      2:02
                    
                    
                      what happened to declarative base?
                      2:05
                    
                    
                      This line right here is
taking care of all of that.
                      2:07
                    
                    
                      Pretty cool, right?
                      2:12
                    
                    
                      So there's a few changes we're going
to have to make to our models.py file.
                      2:14
                    
                    
                      If it helps, keep this
documentation open for reference.
                      2:19
                    
                    
                      I would also bookmark it for future use.
                      2:23
                    
                    
                      Let's create a new file called models.py.
                      2:26
                    
                    
                      At the top,
we need to do our from flask import Flask,
                      2:36
                    
                    
                      and then from flask-sqlalchemy import
                      2:43
                    
                    
                      SQLAlchemy, with a capital SQL and A.
                      2:49
                    
                    
                      And we're also going to need datetime.
                      2:54
                    
                    
                      So I'm gonna go ahead and
import that at the top too,
                      2:59
                    
                    
                      just since we're up here already.
                      3:02
                    
                    
                      Now, if you remember our documentation,
                      3:04
                    
                    
                      we need to move our app
variable over to this file.
                      3:07
                    
                    
                      So we're gonna need a couple of spaces,
and then we're gonna need to take this.
                      3:10
                    
                    
                      And I'm gonna cut it, Make sure I save
that file and paste it over here.
                      3:15
                    
                    
                      Awesome.
                      3:23
                    
                    
                      Next, we're going to need to set up
the database file name and location.
                      3:26
                    
                    
                      This will be app.config.
                      3:29
                    
                    
                      And this is going to be,
I'm gonna check the documentation and
                      3:33
                    
                    
                      I'll do this so we can see compared
to what our current code is.
                      3:38
                    
                    
                      So it's going to need brackets and
                      3:43
                    
                    
                      SQLALCHEMY_DATABASE_URI and
then our location.
                      3:45
                    
                    
                      So actually, I'm just gonna copy this,
Ctrl+C, Ctrl+V.
                      3:50
                    
                    
                      There we go.
                      3:54
                    
                    
                      And then we'll need a string,
and this will be sqlite, colon,
                      3:56
                    
                    
                      three forward slashes, and
then let's call it pets.db.
                      4:02
                    
                    
                      And finally, the last thing we need
to add is our DB variable equals,
                      4:07
                    
                    
                      and this will be SQLAlchemy,
and we're gonna pass in app.
                      4:12
                    
                    
                      Now we can create our model for
the database.
                      4:17
                    
                    
                      Remember, the model creates the layout for
the table by stating what the column names
                      4:20
                    
                    
                      are and
the type of content inside each column.
                      4:24
                    
                    
                      Do a couple spaces, and
this will be class pet, and
                      4:30
                    
                    
                      we're gonna pass in db.Model instead
of base, colon, and let's get inside.
                      4:35
                    
                    
                      Now, we'll need to list out our columns,
                      4:44
                    
                    
                      which will be the different
inputs in our form.
                      4:46
                    
                    
                      First, let's create our primary key field.
                      4:49
                    
                    
                      This will be id = db.Column,
                      4:52
                    
                    
                      And it's going to be a db.Integer, and
                      5:01
                    
                    
                      it's going to be primary_key=True.
                      5:06
                    
                    
                      Awesome.
                      5:15
                    
                    
                      Scroll a bit.
                      5:18
                    
                    
                      Second, let's also create a field to hold
                      5:18
                    
                    
                      a timestamp when this pet
was added to our database.
                      5:21
                    
                    
                      We can call it created,
set it equal to db.Column.
                      5:26
                    
                    
                      And I'm gonna name this one Created,
                      5:33
                    
                    
                      that'll be the name of this column
instead of lowercase created.
                      5:36
                    
                    
                      It's going to be db.DateTime,
                      5:41
                    
                    
                      because it's going to
be a date-time object.
                      5:45
                    
                    
                      And we're gonna give it a default,
which is going
                      5:50
                    
                    
                      to be datetime.datetime.now.
                      5:55
                    
                    
                      And now,
                      6:01
                    
                    
                      typically you see datetime.datetime.now
with the parentheses at the end.
                      6:02
                    
                    
                      But you wanna make sure you don't include
those parentheses, you just want .now.
                      6:06
                    
                    
                      If you did have the parentheses,
                      6:12
                    
                    
                      it would call the function right
away when the models.py file is run.
                      6:15
                    
                    
                      And then it would never call it again,
which is not what we want.
                      6:19
                    
                    
                      We want the date to work every
single time we create a new entry.
                      6:25
                    
                    
                      This is a very common error
developers of all levels make.
                      6:31
                    
                    
                      So if you're taking notes,
highlight, star, or bold that.
                      6:35
                    
                    
                      I'm gonna save, come down to a new line.
                      6:40
                    
                    
                      Okay, now we'll need to create a field for
each item in our pet form.
                      6:44
                    
                    
                      This is gonna be a little bit of
typing now, so let's get to it.
                      6:48
                    
                    
                      And they're also all going
to be string columns,
                      6:52
                    
                    
                      since they all have short messages.
                      6:56
                    
                    
                      Except for the description,
which will be text, so
                      6:58
                    
                    
                      the user can put in a nice
long description for the pet.
                      7:02
                    
                    
                      So we have name = db.Column, there we go.
                      7:06
                    
                    
                      And that's gonna be db.String.
                      7:11
                    
                    
                      And string has a parenthesis so
that you can pass in, like, say,
                      7:13
                    
                    
                      50, if you only want a name to be
allowed to have 50 characters.
                      7:19
                    
                    
                      I'm not gonna set a limit,
I'm just gonna leave it as blank.
                      7:25
                    
                    
                      And at the beginning here,
                      7:29
                    
                    
                      let's also give it a string with
Name as the name of the column.
                      7:32
                    
                    
                      Okay, I'm gonna copy this line because,
                      7:37
                    
                    
                      like I said before,
these are all gonna be extremely similar.
                      7:40
                    
                    
                      And this will just help
us speed up a little bit.
                      7:44
                    
                    
                      So after name is age,
                      7:46
                    
                    
                      let me change this to age.
                      7:48
                    
                    
                      And then the next one,
                      7:52
                    
                    
                      breed, Breed, and
                      7:56
                    
                    
                      then color and Color, and
                      8:02
                    
                    
                      then size and Size, and
                      8:10
                    
                    
                      then weight, Weight.
                      8:17
                    
                    
                      And then after weight is the url,
                      8:26
                    
                    
                      URL, and then it's the url alt tag.
                      8:33
                    
                    
                      I'm just gonna call url_tag.
                      8:38
                    
                    
                      Then after that, we have the pet type.
                      8:45
                    
                    
                      Then after pet type, we have the gender.
                      8:57
                    
                    
                      Then we have whether or
not it's been spayed.
                      9:07
                    
                    
                      And then we have house trained.
                      9:15
                    
                    
                      And then finally is our description.
                      9:24
                    
                    
                      And then remember, this one is going
to be a text instead of a string.
                      9:27
                    
                    
                      So we'll need to change this to db.Text.
                      9:34
                    
                    
                      Whew, that was a lot of typing, but
all of our fields have been defined.
                      9:41
                    
                    
                      Now we've got our model all set up.
                      9:44
                    
                    
                      Let's also add a dunder-repr so
                      9:47
                    
                    
                      our pets are nicely organized when
they're printed to the console.
                      9:49
                    
                    
                      I'm gonna add a space here,
and then def, dunder,
                      9:54
                    
                    
                      repr, it will take self,
because it is a class.
                      9:58
                    
                    
                      And then inside,
we're going to return and f, and
                      10:04
                    
                    
                      I'm gonna do triple string,
we're gonna have Pet.
                      10:09
                    
                    
                      Then we're gonna have the Name
is equal to self.name.
                      10:14
                    
                    
                      And I'm gonna close out my triple string,
enter this onto the next line here.
                      10:20
                    
                    
                      There we go.
                      10:27
                    
                    
                      Then let's do Age is equal to self.age.
                      10:28
                    
                    
                      And then we have Breed, self.breed.
                      10:34
                    
                    
                      And you can see,
I'm gonna do this for all of them.
                      10:37
                    
                    
                      And lastly,
                      10:53
                    
                    
                      Description,
                      10:58
                    
                    
                      self.description.
                      11:04
                    
                    
                      Awesome.
                      11:13
                    
                    
                      Make sure to save your file.
                      11:15
                    
                    
                      And then over in app.py, we'll need
to change out our imports a bit.
                      11:16
                    
                    
                      We no longer need to import capital Flask
because we're importing it over here.
                      11:21
                    
                    
                      And we also need to bring over our app.
                      11:27
                    
                    
                      So let's remove, Flask from there.
                      11:33
                    
                    
                      And then below,
we'll need from models import,
                      11:38
                    
                    
                      and we'll need access to the db variable.
                      11:43
                    
                    
                      We'll talk about that more soon.
                      11:48
                    
                    
                      Need access to our Pet model and
to the app variable.
                      11:50
                    
                    
                      Make sure and save, and
then let's scroll down here.
                      11:56
                    
                    
                      And inside our dunder main,
                      12:01
                    
                    
                      we're going to call db.create_all.
                      12:05
                    
                    
                      This replaces the base.metadata.create_all
call that you've
                      12:10
                    
                    
                      seen in the SQLAlchemy course.
                      12:15
                    
                    
                      Let's save and let's go ahead and
run the file to test it all out.
                      12:17
                    
                    
                      Python app.py, and,
It's saying I can't import pet.
                      12:21
                    
                    
                      It's probably because
I gave it a lowercase.
                      12:24
                    
                    
                      Let's fix that, save.
                      12:33
                    
                    
                      Our classes should always start with
an uppercase, and let's run again.
                      12:36
                    
                    
                      There we go, now it's working.
                      12:39
                    
                    
                      And you can see,
our pets database was created.
                      12:41
                    
                    
                      Nice job, Pythonistas!
                      12:44
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up