我正在尝试使用Flask-SQLAclchemy
和Postgres
为用户类模型创建数据库表。
我的模特课。
from app import db
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(70), index=True, nullable=False)
email = db.Column(db.String(70), index=True, unique=True, nullable=False)
password = db.Column(db.String(128))
我的应用初始化。
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object('app.instance.config.DevelopmentConfig')
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:RandomPass@localhost/BrightEventDb'
SQLALCHEMY_TRACK_MODIFICATIONS = True
db = SQLAlchemy(app)
会记录以下信息。
2018-01-06 11:53:09,978 INFO sqlalchemy.engine.base.Engine select version()
2018-01-06 11:53:09,979 INFO sqlalchemy.engine.base.Engine {}
2018-01-06 11:53:09,982 INFO sqlalchemy.engine.base.Engine select current_schema()
2018-01-06 11:53:09,982 INFO sqlalchemy.engine.base.Engine {}
2018-01-06 11:53:09,984 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1
2018-01-06 11:53:09,985 INFO sqlalchemy.engine.base.Engine {}
2018-01-06 11:53:09,986 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1
2018-01-06 11:53:09,987 INFO sqlalchemy.engine.base.Engine {}
2018-01-06 11:53:09,990 INFO sqlalchemy.engine.base.Engine show standard_conforming_strings
2018-01-06 11:53:09,991 INFO sqlalchemy.engine.base.Engine {}
答案 0 :(得分:2)
正如提及的评论中的某些人您未将模型导入应用初始化模块。您可以按照以下方式执行此操作
from models import User
答案 1 :(得分:2)
如果您正在关注烧瓶快速启动最小应用程序,则该命令默认工作,因为User
类与db
实例位于同一位置。但是,在您的情况下,您必须将评论User
中提到的from models import User
课程导入应用初始化