Flask创建多个数据库
创建bases.py基类数据
# mybooks/app/models/bases.py
db = SQLAlchemy()
class MyBase(db.Model):
__abstract__ = True
create_time = Column('create_time', Integer)
status = Column(SmallInteger, default=1)
创建wish.py模型
# mybooks/app/models/wish.py
class Wish(MyBase):
__tablename__ = 'wish'
id = Column(Integer, primary_key=True)
launched = Column(Boolean, default=False)
user = relationship('User')
uid = Column(Integer, ForeignKey('user.id'), nullable=True)
isbn = Column(String(15), nullable=False)
创建user.py模型
# mybooks/app/models/user.py
class User(MyBase):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
nickname = Column(String(24), nullable=False)
phone_number = Column(String(18), unique=True)
email = Column(String(50), unique=True, nullable=False)
_password = Column('password', String(128), nullable=True)
confirmed = Column(Boolean, default=False)
beans = Column(Float, default=0)
send_counter = Column(Integer, default=0)
receive_counter = Column(Integer, default=0)
gifts = relationship('Gift')
创建gift.py模型
# mybooks/app/models/gift.py
class Gift(MyBase):
__tablename__ = 'gift'
id = Column(Integer, primary_key=True)
launched = Column(Boolean, default=False)
user = relationship('User')
uid = Column(Integer, ForeignKey('user.id'), nullable=True)
isbn = Column(String(15), nullable=False)
模型的__init__.py文件,model中有哪些模型文件就添加多少
# mybooks/app/models/__init__.py
from .bases import *
from .gift import *
from .user import *
from .wish import *
创建app对象文件__init__.py
# mybooks/app/__init__.py
def create_app():
app = Flask(__name__)
app.config.from_object("app.secure")
db.init_app(app)
db.create_all(app=app)
return app
创建构建数据库的文件manage_server.py
# mybooks/manage_server.py
app = create_app()
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command("db", MigrateCommand)
if __name__ == "__main__":
manager.run()