Russ <russandheat...@gmail.com> wrote:

> I should have also indicated that the addition of sqlalchemy.sql.text fixes 
> the small mixin example.  The little script below works, but I don't know if 
> it is a sketchy hack, or a safe long term solution:
> 
> from sqlalchemy import * 
> from sqlalchemy.orm import * 
> from sqlalchemy.ext.declarative import declarative_base, declared_attr 
> from sqlalchemy.sql import text as sql_text
> 
> Base = declarative_base() 
> 
> class A_TableDef(object): 
>     __tablename__ = 'a'
>     
>     id = Column(Integer, primary_key=True)
>     track_type       = Column(SmallInteger, nullable = False)
> 
>     @declared_attr
>     def __table_args__(cls):
>         return (Index("idx_track_type2", "track_type",
>                       postgresql_where = sql_text("track_type != 0")),
>                 )
> 
> class A_Model(Base, A_TableDef):
>     pass
> 
> e = create_engine("postgresql://postgres:postgrespw@localhost:5433/edms", 
> echo =True)
> 
> Base.metadata.drop_all(e)
> Base.metadata.create_all(e)


with mixins, this will work as is in latest master, see
http://docs.sqlalchemy.org/en/latest/changelog/migration_10.html#improvements-to-declarative-mixins-declared-attr-and-related-features.

In 0.9, the declared_attr here is called sooner than we’d like, though this
particular example works if we just give the column a name (more complex
things will still not work very well with the mixins here though):

class A_TableDef(object):
    __tablename__ = 'a'
    id = Column(Integer, primary_key=True)

    track_type = Column('track_type', SmallInteger, nullable = False)

    @declared_attr
    def __table_args__(cls):
        return (Index("idx_track_type", "track_type",
                      postgresql_where=(cls.track_type != 0)),
                )

The version with text() is perfectly fine as postgresql_where isn’t significant 
anywhere except in the DDL.



> 
> 
> -- 
> 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.

Reply via email to