The SQLAlchemy kernel does not complain about the lack of a primary key, so you can create such a table using Table(…) . But ORM by design requires a way to identify the row corresponding to the object, so it is impossible to use a table without a primary key in ORM.
Why do you need to add this index later? Is this a real requirement or an attempt to solve a problem that can probably be solved in another way? If you need a composite primary key, you can define it using the primary_key=True argument in multiple Column or by specifying PrimaryKeyConstraint(…) in __table_args__ .
source share