I am upgrading from SQLAlchemy 1.4 to SQLAlchemy 2 and I am running into an error: Can't add additional column 'machine_id' when specifying __table__
when trying to create a versioned history table with inherited columns. Following the example SQLAlchemy example:
versioned_cls = type(
"%sHistory" % cls.__name__,
bases,
{
"_history_mapper_configured": True,
"__table__": history_table,
"__mapper_args__": dict(
inherits=super_history_mapper,
polymorphic_identity=local_mapper.polymorphic_identity,
polymorphic_on=polymorphic_on,
properties=properties,
),
},
)
class MachineIdMixin(object):
@declared_attr.cascading
def machine_id(self) -> Mapped[String]:
return mapped_column(String, ForeignKey("machines.id"), nullable=False, index=True)
class MyTable(Base, MachineIdMixin, Versioned):
__table__ = "mytable"
id = Column(String, primary_key=True)
When attempting to run however I get the following error: Can't add additional column 'machine_id' when specifying __table__
. When debugging on that line, the passed in column is machine_id
whereas mytable_history
as a column mytable_history.machine_id
, and it incorrectly identifies as a column to be added. I've also checked: https://docs.sqlalchemy.org/en/20/orm/declarative_mixins.html#using-orm-declared-attr-to-generate-table-specific-inheriting-columns and it seems the mixin definition is correct. Is there something I am doing incorrectly?