0

ELI5: When I run db.py from CLI, the db.db file is created, but it's 0 KB which makes me think the schema was not created. What can I do to get the model created in the db?

db.py (Edited: 2020-07-22 1007 CST)

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

if __name__ == '__main__':
    from app import app

    db.init_app(app)

    app.app_context().push()

    from models import Property

    db.create_all()
    db.session.commit()

    home = Property(
        street_address="123 Valentine Dr",
        city = "Long Beach",
        state = "MS",
        postal_code = "39560",
        title = "Home"
    )

    db.session.add(home)
    db.session.commit()

models.py (Edited: 2020-07-22 1007 CST)**

from db import db
import datetime


class Property(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(256), nullable=False)
    street_address = db.Column(db.String(256), nullable=False)
    city = db.Column(db.String(256), nullable=False)
    state = db.Column(db.String(256), nullable=False)
    postal_code = db.Column(db.String(256), nullable=False)
    timestamp = db.Column(db.DateTime, default=datetime.datetime.now)

    def __init__(self, title, street_address, city, state, postal_code):
        self.title = title
        self.street_address = street_address
        self.city = city
        self.state = state
        self.postal_code = postal_code

app.py (Edited: 2020-07-24 12:01 CST)

from flask import Flask, render_template
from ll.ll import ll

app = Flask(__name__)

app.register_blueprint(ll, url_prefix="/ll")

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

@app.route("/")
def index():
    return render_template("home.html")

if __name__=='__main__':
    app.run(debug=True, port="5050")
RobbieS
  • 120
  • 12
  • Don't you have to invoke `Property()` explicitly somewhere? See [this](https://stackoverflow.com/questions/20744277/sqlalchemy-create-all-does-not-create-tables), the answer. – Mike O'Connor Jul 16 '20 at 20:36
  • @MikeO'Connor I reorganized my code so the models are imported after db.init_app and the context push are called, but to no avail – RobbieS Jul 16 '20 at 21:47
  • If you look at that example--- I'm talking about the answer by rocknrollnerd-- there is the statement `admin = User('admin', 'admin@example.com')`, and another like it. Those are instantiations of what would be in your case the `Property` class. Otherwise no use is made of that class. – Mike O'Connor Jul 17 '20 at 05:58
  • Here's another [example](https://pythonbasics.org/flask-sqlalchemy/). – Mike O'Connor Jul 17 '20 at 06:48
  • @MikeO'Connor: I updated my code to reflect what rocknrollnerd posted. The traceback still says the table does not exist when I call db.py from the CLI. Thoughts? – RobbieS Jul 22 '20 at 15:10
  • I'm not familiar with what `app.app_context().push()` does but note that with both of the examples that I cited there is an `app.config()` assignment that tells the code where the database file is. You don't have that. – Mike O'Connor Jul 24 '20 at 05:26

0 Answers0