迁移模式与种子表

2024-04-19 00:51:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我想确认一下迁移模式与种子初始数据之间的关系

目前,我正在使用Python和Flask迁移以及Flask Restful+Flask蓝图

因此,在我的代码中,我目前所做的是:

  1. 已在模型中定义架构

    导入日期时间 导入uuid 从应用程序导入数据库 来自sqlalchemy.dialogs.postgresql导入UUID 从sqlalchemy.event导入侦听 从sqlalchemy.orm导入关系,backref 从flask_security.utils导入哈希_密码

    班级奖励(db.Model): 表名=“奖励” id=db.Column(db.Integer,主键=True) name=db.Column(db.String) image\u url=db.Column(db.String,nullable=True) 已在=db.Column处创建(db.DateTime,默认值=db.func.current\u timestamp()) 更新的\u at=db.Column(db.DateTime,nullable=True,onupdate=db.func.current\u timestamp())

所以在app的init.py中,我定义了它

# from authlib.integrations.flask_client import OAuth
from flask import Flask
from flask_cors import CORS
from flask_jwt_extended import JWTManager
from flask_restful import Api
from flask_security import Security, SQLAlchemySessionUserDatastore
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow

# Create package object. 
db = SQLAlchemy()
ma = Marshmallow()
jwt = JWTManager()
api = Api() 
security = Security()

# Initialize the package that needed
def init_app(name, config=None):
    app = Flask(name)
    app.config.from_object(config)
    db.init_app(app)
    ma.init_app(app)
    api.init_app(app)
    CORS(app)
    security.init_app(app)
    

    from .models.award_model import Award

所以我需要做的是给这些数据添加一个种子数据。 但对于目前的问题,我不知道如何用正确的方式来定义它

有什么建议吗

我认为:

  1. 在模型中创建批量插入
  2. 创建用于种子设定的命令行代码(如laravel)

Tags: 数据namefromimporttrueappflaskdb