我正在开发一个分类广告网站,使用 Flask 和 mysql 作为数据库。我对数据库模型做了一些更改。例如,我在 ForSaleAd 中添加了价格、品牌、型号。我尝试将更改升级到mysql数据库。我在 init.py 中运行 db.init_app 后添加迁移设置。但是,数据库中没有任何变化。 这是项目结构:
classified_ads
|
|___app
| |
| |_static
| |
| |_templates
| |
| |__init.py__
| |
| |__category.py
| |
| |__model.py
|
|___myenv
|
|___config.py
|
|___main.py
init.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from config import Config
from flask_migrate import Migrate
db = SQLAlchemy()
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
def create_app():
app = Flask(__name__)
#load configuration files
app.config.from_object(Config)
db.init_app(app)
#migrate changes to database
migrate = Migrate(app, db)
migrate.init_app(app, db)
from .model import User,init_categories
with app.app_context():
db.create_all()
init_categories()
login_manager.init_app(app)
@login_manager.user_loader
def load_user(user_id):
return User.get(user_id)
return app
model.py
from app import db
from flask_login import UserMixin
from werkzeug.security import generate_password_hash,check_password_hash
#represents a registered user
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(120), unique=True, nullable=False)
password_hash = db.Column(db.String(128))
...
#ad's main category
class Category(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True, nullable=False)
subcategories = db.relationship('Subcategory')
#ad's subcategory
class Subcategory(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True, nullable=False)
category_id = db.Column(db.Integer, db.ForeignKey('category.id'), nullable=False)
#base ad with common fields provide template
class Ad(db.Model):
__abstract__ = True
id = db.Column(db.Integer, primary_key=True)
subject = db.Column(db.String, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
subcategory_id = db.Column(db.Integer, db.ForeignKey('subcategory.id'), nullable=False)
...
class HousingAd(Ad):
__tablename__ = 'housing_ad'
rent = db.Column(db.Float)
rent_period = db.Column(db.Enum('day', 'week', 'month'))
type = db.Column(db.Enum('room', 'house', 'homestay', 'vacation', 'other'))
class ForSaleAd(Ad):
__tablename__ = 'for_sale_ad'
price = db.Column(db.Float)
brand = db.Column(db.String)
make = db.Column(db.String)
model = db.Column(db.String)
condition = db.Column(db.Enum('new','used','deficiency'))
class JobAd(Ad):
__tablename__ = 'job_ad'
industry = db.Column(db.String)
compensation = db.Column(db.String)
class Payment(db.Model):
id = db.Column(db.Integer, primary_key=True)
card_number = db.Column(db.String(16), nullable=False)
...
#create category and subcategory names
from . import category
def init_categories():
for cat_name, subcats in category.categories.items():
categry = Category.query.filter_by(name=cat_name).first()
if not categry:
categry = Category(name=cat_name)
db.session.add(categry)
db.session.commit()
for subcat_name in subcats:
subcat = Subcategory.query.filter_by(name=subcat_name, category_id=categry.id).first()
if not subcat:
subcat = Subcategory(name=subcat_name, category_id=categry.id)
db.session.add(subcat)
db.session.commit()
category.py
categories = {
"Housing": [
"Housing available",
"Housing wanted",
"Other"
],
"For Sale": [
"Appliance",
"Auto",
"Clothes",
"Computer",
"Phone",
"Electronics",
"Free",
"Household",
"Transportation",
"Musical Instrument",
"Wanted",
"Other"
],
"Jobs": [
"Admin Office",
"Customer Service",
"Driver",
"Food Restaurant",
"General Labor",
"Job Seeking",
"Landscaping",
"Massage",
"Nanny",
"Warehouse",
"Software IT",
"Other"
]
}
config.py
class Config:
SECRET_KEY = 'dev'
SQLALCHEMY_DATABASE_URI ='mysql+pymysql://root:psword@localhost/databasename'
main.py
from flask import render_template,url_for
from app import create_app
from app import category
app = create_app()
bunch of routes...
我找到了解决办法。我之前更改了文件夹名称,这就是代码不起作用的原因。因此,我更新 Flask 配置文件中的文件夹名称以识别新路径。然后我使用
flask db init; flask db migrate -m 'initial changes'; flask db upgrade;
来升级数据库。