Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Flask with SQLAlchemy Basics Connecting to a Database with SQLAlchemy SQLAlchemy and Flask

Why is my .db file not being created?

Why is my .db file not being created? I am using Flask SQLAlchemy, but I am having some problems. Here is my code:

from flask import Flask, render_template

from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///test.db'

db = SQLAlchemy(app)

It runs without error, but the .db file is not being created. Any ideas why?

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 67,893 Points

You are almost there. From the quickstart document page it says:

To create the initial database, just import the db object from an interactive Python shell and run the
SQLAlchemy.create_all() method to create the tables and database:

>>> from yourapplication import db
>>> db.create_all()

So if you above file is called myapp.py you would use: from myapp import db

The db is the object you created in file myapp.py, line 9.

Post back if you need more help. Good luck!!