python - Alembic - sqlalchemy 初始迁移

标签 python django sqlalchemy alembic

我在创建初始迁移时遇到问题,该迁移将自动包含我在 models.py 中使用共享基础(declarative_base)定义的表。

当我输入命令时:

alembic revision --autogenerate

alembic 创建一个空文件。

我的配置或方法有什么问题?

项目.base.py:
from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base()

环境.py:
import sys
import os

sys.path.append(os.path.abspath(os.getcwd()))
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig

from project.base import Base
target_metadata = Base.metadata
def run_migrations_online():
    """Run migrations in 'online' mode.

    In this scenario we need to create an Engine
    and associate a connection with the context.

    """
    engine = engine_from_config(
        config.get_section(config.config_ini_section),
        prefix='sqlalchemy.',
        poolclass=pool.NullPool)

    connection = engine.connect()
    context.configure(
        connection=connection,
        target_metadata=target_metadata
    )

    # target_metadata.reflect(engine, only=[
    #     "django_migrations",
    #     "auth_group_permissions",
    #     "django_session",
    #     "auth_user_user_permissions",
    #     "auth_user_groups",
    #     "django_admin_log",
    #     "auth_permission",
    #     "auth_user",
    #     "sysdiagrams",
    #     "django_content_type",
    #     "auth_group",
    #     "sysdiagrams",
    # ])

    try:
        with context.begin_transaction():
            context.run_migrations()
    finally:
        connection.close()


if context.is_offline_mode():
    run_migrations_offline()
else:
    run_migrations_online()

sample 型号:
# -*- coding: utf-8 -*-

from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey, SmallInteger
from sqlalchemy.orm import relationship, backref
from project.base import Base


__schema__ = "Users"


class User(Base):
    __tablename__ = "User"
    __table_args__ = {'schema': __schema__}

    USER_CUSTOMER = 0
    USER_EMPLOYEE = 5
    USER_ADMIN = 10

    USER_TYPES = (
        (USER_CUSTOMER, u'Klient'),
        (USER_EMPLOYEE, u'Obsługa sklepu'),
        (USER_ADMIN, u'Administrator')
    )

    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    email = Column(String(255))
    password = Column(String)
    date_created = Column(DateTime)
    date_updated = Column(DateTime)
    user_type = Column(SmallInteger)

    is_active = Column(Boolean)

    def __repr__(self):
        return u"<User: ({} {})>".format(self.id, self.name)

    def is_management(self):
        return self.user_type in [self.USER_EMPLOYEE, self.USER_ADMIN]

    def is_admin(self):
        return self.user_type == self.USER_ADMIN

编辑:

我发现 Base.metadata.sorted_tables是空的。

最佳答案

除了导入您的声明 Base类,您还需要导入所有模型。像 import project.models或您必须包含的任何模块,以便导入所有模型类。否则,您的 Base.metadataenv.py 以来,您的模型定义不再填充从不包括它们。

关于python - Alembic - sqlalchemy 初始迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26251094/

相关文章:

python - 如何在 X_train、y_train、X_test、y_test 中分割图像数据集?

python - 查找图中顶点的可达顶点

python - 如何仅使用一个序列化器MethodField 获取两个值

python - 如何使用 django-treebeard 构建站点地图?

sqlalchemy - 如何使用 Flask-SQLAlchemy 制作可重用的组件?

python - 在流式传输 Flask 响应时保持 SQLAlchemy session 处于事件状态

python - 如何使用装饰器使蜘蛛能够区分 scrapy 管道

python - 如何检查opencv是否使用GPU?

python - PyCharm docker 调试错误

python - 用于列出 python 模块中所有方法的 Emacs 插件