support __table_args__ on single inh classes

Issue #2700 new
Michael Bayer
repo owner created an issue

since we support columns on them too.


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

class FixTableArgs(object):

@event.listens_for(FixTableArgs, "instrument_class", propagate=True)
def extra_table_args(mapper, cls):
    if '__extra_table_args__' in cls.__dict__:
        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"), )

print BaseClass.__table__.constraints

Comments (6)

  1. Anonymous

    The workaround works fine, thanks.

    However, I noticed that the explicit configure_mappers() call is now required, otherwise Base.metadata.create_all() will miss constraints declared using this workaround. Actually, it's weirder than that, because I found a case when __extra_table_args__ was a tuple of a CHECK and a UNIQUE constraint, the CHECK constraint was created, the UNIQUE constraint was not. Calling configure_mappers() before Base.metadata.create_all() seems to sort things out. I've used 0.8.0 for testing.

  2. Log in to comment