I have been following a FLask/SQLalchemy tutorial over at talkpython, but I can't get SQLalchemy to create my SQLite database.
AT this moment I am in doubt if it's running the main method at all, int he terminal debug is set to 0 and the print statement from main() is never printed.
I really don't know what I have been doing wrong, the site lunches and I can browse it, but no SQLite file is ever created in the db folder.
app.py:
import os
import flask
import data.db_session as db_session
app = flask.Flask(__name__)
def main():
print("print this if coming from main()")
setup_db()
app.run(debug=True)
def setup_db():
db_file = os.path.join(
os.path.dirname(__file__),
'db',
'database.sqlite')
db_session.global_init(db_file)
@app.route('/')
def frontpage():
return flask.render_template('index.html')
if __name__ == '__main__':
main()
db_session.py:
import sqlalchemy as sa
import sqlalchemy.orm as orm
from data.modelbase import SqlAlchemyBase
factory = None
def global_init(db_file: str):
global factory
if factory:
return
if not db_file or not db_file.strip():
raise Exception("You must specify a db file.")
conn_str = 'sqlite:///' + db_file.strip()
print("Connecting to DB with {}".format(conn_str))
engine = sa.create_engine(conn_str, echo=False, connect_args={"check_same_thread": False})
factory = orm.sessionmaker(bind=engine)
# noinspection PyUnresolvedReferences
from data.location import Location
SqlAlchemyBase.metadata.create_all(engine)
Terminal output:
FLASK_APP = app.py
FLASK_ENV = development
FLASK_DEBUG = 0
In folder C:/Users/******/OneDrive/*********
"C:\Users\****\Anaconda3\envs\********\python.exe" -m flask run
* Serving Flask app "app.py"
* Environment: development
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)