this feature should be added and is http://www.sqlalchemy.org/trac/ticket/2700, 
 here's a workaround for now:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

class FixTableArgs(object):
    @classmethod
    def __declare_last__(cls):
        if '__extra_table_args__' in cls.__dict__:
            cls.__table__._init_items(*cls.__extra_table_args__)
            del cls.__extra_table_args__

Base = declarative_base(cls=FixTableArgs)

class BaseClass(Base):
    __tablename__ = "base"

    id = Column(Integer, primary_key=True)
    base_col = Column(Integer)

    __table_args__ = (Index("ix1", base_col),)

class SubClass(BaseClass):
    col_a = Column(Integer)
    col_b = Column(Integer)

    __extra_table_args__ = (UniqueConstraint("col_a", "col_b"), )

configure_mappers()

print BaseClass.__table__.constraints




On Apr 9, 2013, at 5:38 AM, "Gombas, Gabor (IT)" 
<[email protected]> wrote:

> Hi,
> 
> What is the recommended method of specifying constraints on columns
> added by a subclass using single-table inheritance? This does not work,
> I get a KeyError for "col_a":
> 
> class BaseClass(Base)
>    __table__ = "base"
>    __table_args__ = (Index("base_col"),
>                      UniqueConstraint("col_a", "col_b"))
> 
>    base_col = Column(Integer)
> 
> class SubClass(BaseClass):
>    col_a = Column(Integer)
>    col_b = Column(Integer)
> 
> SQLA does not allow __table_args__ placed on SubClass. I can do a
> workaround by using BaseClass.__table__.append_constraint() after
> defining SubClass, but mixing different ways of defining constraints
> does not look nice.
> 
> Gabor
> 
> -- 
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
> 
> 

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to