python - SQLAlchemy: Possible to declare a column as primary key declaratively? -
i have base class mixin:
class mycolumns(object): id = column(integer) foo = column(integer) bar = column(integer) class mymaintable(mycolumns, base): __tablename__ = 'main_table' pk_id = primarykeyconstraint("id") i want able declare id pk on mymaintable. can't declare pk within mycolumns, because need use mycolumns in table, id not pk (done auditing purposes). when run above code, get
sqlalchemy.exc.argumenterror: mapper mapper|mymaintable|main_table not assemble primary key columns mapped table 'main_table' is there way add pk declaration way?
i found solution documented here: http://docs.sqlalchemy.org/en/rel_0_9/core/constraints.html#setting-up-constraints-when-using-the-declarative-orm-extension
you need add constraints __table_args__:
class mycolumns(object): id = column(integer) foo = column(integer) bar = column(integer) class mymaintable(mycolumns, base): __tablename__ = 'main_table' __table_args__ = ( primarykeyconstraint("id", name="pk_id"), )
Comments
Post a Comment