1 year ago

#380169

test-img

Hiranya Agarwal

Flask SQLalchemy creating empty database

first time poster, learning python at school.

I am building a flask app and trying to set up a database using SQLalchemy. When I run the app the database is created but has no structure(tables).

my app.py looks like this:

from flask import Flask, render_template, 
from flask_sqlalchemy import SQLAlchemy
from model import *

app = Flask(__name__)
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI']= 'sqlite:///app.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
db = SQLAlchemy(app)
db.init_app(app)
db.create_all()

my model.py looks like this:

from datetime  import datetime
import db

class Post(db.Model):
    id=db.column(db.interger(), primary_key=True)
    title=db.column(db.string(225),nullable=False)
    slug=db.column(db.string(225), nullable=False)
    content=db.column(db.text(), nullable=False)
    created_on = db.column(db.DateTime(), default=datetime.utcnow)
    updated_on=db.column(db.DateTime(), default=datetime.utcnow, onupdate=datetime )

I believe the database is not connected to the model.py file.

python

flask

flask-sqlalchemy

0 Answers

Your Answer

Accepted video resources