2014-10-08 5 views
3

공유베이스 (declarative_base)를 사용하여 내 models.py에서 정의한 테이블이 자동으로 생성되는 초기 마이그레이션을 만드는 데 문제가 있습니다.Alembic - sqlalchemy 초기 마이그레이션

alembic revision --autogenerate 

증류기 빈 파일을 만듭니다 : 나는 명령을 입력

.

내 구성 또는 내 접근 방식에있어 잘못된 점은 무엇입니까?

project.base.py :

from sqlalchemy.ext.declarative import declarative_base 


Base = declarative_base() 

env.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() 

샘플 모델 :

# -*- 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 

편집 :

나는 것을 발견 한 자료 .metadata.sorted_tables가 비어 있습니다.

답변

7

선언적 Base 클래스를 가져 오는 것 외에도 모든 모델을 가져와야합니다. import project.models 또는 모든 모델 클래스를 가져올 수 있도록 포함해야하는 모듈이 있어야합니다. 그렇지 않은 경우 env.py에 모델 정의가 포함되지 않으므로 Base.metadata에 모델 정의가 채워지지 않습니다.

+0

굉장! 그 트릭을 했어! – Efrin