I have a 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 to be able to declare id as the PK on MyMainTable. I can't declare it as PK within MyColumns, because I need to use MyColumns in another table, where id is NOT the PK (done for auditing purposes). When I run the above code, I get

sqlalchemy.exc.ArgumentError: Mapper Mapper|MyMainTable|main_table could not assemble any primary key columns for mapped table 'main_table' 

Is there any way to add the PK declaration this way?

1 Answer

I found the solution as documented here:

You need to add the constraints to __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"), ) 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.