2016-06-08 7 views
0

와 부분 인덱스를 만들어이 인덱스 다음과 같은 모델 :SqlAlchem ​​y의 및 증류기

class User(TableMixin, Base): 
    username = Column(String, nullable=False, unique=False) 
    password = Column(String, nullable=False, unique=False) 

    Index('ix_active_username', "active", username, 
      unique=True, 
      postgresql_where=("active" is True) 
     ) 

TabbleMixin 클래스

class TableMixin(): 
    id = Column(Integer, primary_key=True) 
    active = Column(Boolean, nullable=False, default=True) 

내 포스트 그레스 DB에 생성하고자하는 인덱스 :

CREATE UNIQUE INDEX unique_active_username on user (username, active) 
where active = True; 

사용자 c 내부에서 인식되지 않기 때문에 인식되지 않기 때문에 인용 부호 사이에 액티브가 있습니다. 젊은 여자.

"alembic revision --autogenerate"를 실행 한 후 alembic 파일이 비어 있습니다. 아무도 내가 뭘 잘못하고 있는지 알아?

편집 : 색인의 범위를 벗어난 색인 만 입력하면됩니다. Ilja Everilä에

class User(TableMixin, Base): 
    username = Column(String, nullable=False, unique=False) 
    password = Column(String, nullable=False, unique=False) 

class TableMixin(): 
    id = Column(Integer, primary_key=True) 
    active = Column(Boolean, nullable=False, default=True) 

Index('ix_active_username', User.active, User.username, 
     unique=True, 
     postgresql_where=(User.active == true()) 
    ) 

감사 : 그리고

postgresql_where=(User.active == true()) 

에 모든 코드를 어디에 변경!

+1

글쎄, 우선' "활성"TRUE '를 인수로 전달됩니다'FALSE '로 평가 단지 파이썬 표현입니다. '__table_args__'에서 클래스 본문에'Index'를 생성하고 있습니까? 대신에'Index'를 따로 만들면'User' 모델을 통해 적절한 컬럼에 접근 할 수 있습니다. –

+0

아니요 __table_args__에서 생성하지 않습니다. 나는 그것을 사용자 클래스에서 만들고있다. 클래스 외부에서 생성하려고 할 때이 오류가 발생합니다. " 'bool'개체에 '_compiler_dispatch'특성이 없습니다." – Eelco

+1

또한 'is'를 사용하면 안됩니다. 객체 ID 비교이므로 재정의 할 수 없으므로 적절한 sqla 구문을 사용하더라도 작업하지 않습니다. sqla가 컴파일 할 수없는 인수 * postgresql_where *로'False'를 전달하기 때문에 오류가 발생합니다. –

답변

3

완전한 솔루션 :

class User(TableMixin, Base): 
    username = Column(String, nullable=False, unique=False) 
    password = Column(String, nullable=False, unique=False) 

class TableMixin(): 
    id = Column(Integer, primary_key=True) 
    active = Column(Boolean, nullable=False, default=True) 

Index('ix_active_username', User.active, User.username, 
     unique=True, 
     postgresql_where=(User.active == true()) 
    )