Database is there and gets initialized

This commit is contained in:
Jan 2024-03-14 15:50:36 +01:00
parent 4fc6765b1d
commit d023307ccd
3 changed files with 14 additions and 21 deletions

View File

@ -5,27 +5,29 @@ from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase from sqlalchemy.orm import DeclarativeBase
from sqlalchemy import Integer, String from sqlalchemy import Integer, String
from sqlalchemy.orm import Mapped, mapped_column from sqlalchemy.orm import Mapped, mapped_column
from datetime import datetime
#create the app
app = Flask(__name__) app = Flask(__name__)
# configure the SQLite database, relative to the app instance folder # configure the database, give it a path (it will be in the instances folder)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///database.db" app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///database.db"
# initialize the app with the extension db = SQLAlchemy(app)
db.init_app(app)
data = {}
class Base(DeclarativeBase): # create the model for the response table
pass class Response(db.Model):
id = db.Column(db.Integer, primary_key=True)
likert_result = db.Column(db.Integer, nullable=False)
notes = db.Column(db.String(200))
date_created = db.Column(db.DateTime, default=datetime.utcnow)
class User(db.Model): def __repr__(self) -> str:
id: Mapped[int] = mapped_column(primary_key=True) return "<Task %r>" % self.id
username: Mapped[str] = mapped_column(unique=True)
email: Mapped[str]
# create the table (existing tables are not overwritten)
with app.app_context(): with app.app_context():
db.create_all() db.create_all()
print("Table created")
db = SQLAlchemy(model_class=Base)
@app.route("/", methods=["GET", "POST"]) @app.route("/", methods=["GET", "POST"])
def testpage(): def testpage():

Binary file not shown.

View File

@ -1,9 +0,0 @@
import click
from flask import current_app, g
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase
class Base(DeclarativeBase):
pass
db = SQLAlchemy(model_class=Base)