Russ <russandheat...@gmail.com> wrote:
> What is the proper way to declare a postgresql partial index when using the > @declared_attr decorator? these two concepts aren’t really connected > > This form gives me "Cannot compile Column object until its 'name' is > assigned": > > track_type = Column(SmallInteger, nullable = False) > @declared_attr > def __table_args__(cls): > return (Index("idx_track_type", "track_type", > postgresql_where = (cls.track_type != 0)), > ) > > and this form gives me "AttributeError: 'str' object has no attribute > '_compiler_dispatch’": please give me stack traces. or at least versions. works for me. Here’s 0.9: from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base, declared_attr Base = declarative_base() class A(Base): __tablename__ = 'a' id = Column(Integer, primary_key=True) track_type = Column(SmallInteger, nullable = False) @declared_attr def __table_args__(cls): return (Index("idx_track_type", "track_type", postgresql_where = (cls.track_type != 0)), ) e = create_engine("postgresql://scott:tiger@localhost/test", echo =True) Base.metadata.drop_all(e) Base.metadata.create_all(e) CREATE TABLE a ( id SERIAL NOT NULL, track_type SMALLINT NOT NULL, PRIMARY KEY (id) ) 2015-02-02 17:56:12,610 INFO sqlalchemy.engine.base.Engine {} 2015-02-02 17:56:12,612 INFO sqlalchemy.engine.base.Engine COMMIT 2015-02-02 17:56:12,613 INFO sqlalchemy.engine.base.Engine CREATE INDEX idx_track_type ON a (track_type) WHERE track_type != 0 2015-02-02 17:56:12,613 INFO sqlalchemy.engine.base.Engine {} 2015-02-02 17:56:12,614 INFO sqlalchemy.engine.base.Engine COMMIT > > track_type = Column(SmallInteger, nullable = False) > @declared_attr > def __table_args__(cls): > return (Index("idx_track_type", "track_type", > postgresql_where = "track_type != 0"), > ) > > From [this post][1] I learned about the use of sqlalchemy.sql.text, so this > is now working for me: > > from sqlalchemy.sql import text as sql_text > # <snip> > @declared_attr > def __table_args__(cls): > return (Index("idx_track_type", "track_type", > postgresql_where = sql_text("track_type != 0")), > ) > > That post also indicated there may be a bug here, but that was almost 2 years > ago. Is there a better way to do it now? More importantly, will the working > code above continue to work in the future? > > [1]: http://goo.gl/Fmgynh > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@googlegroups.com. > Visit this group at http://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.