I am trying to add the Product table into the database using flask sqlalchemy. It adds all the columns except for the userBudget Does anyone know why? On the cmd, I do
from app import db
db.create_all()
Then I go into sqlite3 and check the .schema but only the userBudget column is missing
sqlite> .schema
CREATE TABLE user (
id INTEGER NOT NULL,
username VARCHAR(15),
email VARCHAR(50),
password VARCHAR(80),
PRIMARY KEY (id),
UNIQUE (username),
UNIQUE (email)
);
CREATE TABLE product (
"userId" INTEGER NOT NULL,
"productURL" VARCHAR NOT NULL,
"currentPrice" INTEGER,
PRIMARY KEY ("userId"),
FOREIGN KEY("userId") REFERENCES user (id)
);
import requests
from flask import Flask, render_template, redirect, url_for, request
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
# Instantiation
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db' # location of database
SQLALCHEMY_TRACK_MODIFICATIONS = False # no sqlalchemy warnings in console
bootstrap = Bootstrap(app) # allows use of flask-bootstrap
db = SQLAlchemy(app) # database
# Initialization
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
# Database Tables
class User(UserMixin, db.Model): # UserMixin provides default implementations for the methods flask-login expects users to have
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(15), unique=True)
email = db.Column(db.String(50), unique=True)
password = db.Column(db.String(80))
class Product(db.Model):
userId = db.Column(db.Integer, db.ForeignKey(User.id), primary_key=True)
productURL = db.Column(db.String(), nullable=False)
currentPrice = db.Column(db.Integer)
userBudget = db.Column(db.Integer, nullable=False)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
if __name__ == '__main__':
app.run(debug=True)
db.create_all()only creates new tables, it does not migrate changes to existing tables. Might this be the case?