Jan-31-2023, 03:18 PM
Hi,
I am trying to make an e-commerce app using Flask and SQLAlchemy. Whenever I create the databases in my program, the database is getting created, but without the tables from models.py.
Whenever I do
The database is created, but when I check it there are no tables in the database. Here are the relevant files:
models.py
from app import db
from flask_login import UserMixin
I have had a lot of trouble with using databases with Flask in the past, so any advice here would be much appreciated. Thank you!
I am trying to make an e-commerce app using Flask and SQLAlchemy. Whenever I create the databases in my program, the database is getting created, but without the tables from models.py.
Whenever I do
Quote:>>> from app import app, db
>>> app.app_context().push()
>>> db.create_all()
The database is created, but when I check it there are no tables in the database. Here are the relevant files:
models.py
from app import db
from flask_login import UserMixin
class User(db.Model, UserMixin): id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String(20), nullable=False, unique=True) email = db.Column(db.String(50), unique=True, nullable=False) password = db.Column(db.String(60), nullable=False)app.py
from flask import Flask, render_template from flask_bootstrap import Bootstrap from forms import RegisterForm from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db' app.config['SECRET_KEY'] = 'secretly' Bootstrap(app) db = SQLAlchemy(app) @app.route('/') def homepage(): return render_template('index.html') @app.route('/login', methods=['POST','GET']) def login_page(): pass @app.route('/register', methods=['POST','GET']) def register_page(): form = RegisterForm() return render_template('register.html', form=form) if __name__ == '__main__': app.run(debug=True)When I put the User model in app.py rather than in models.py, I notice that the database is created.
I have had a lot of trouble with using databases with Flask in the past, so any advice here would be much appreciated. Thank you!