I'm using declarative in SQLAlchemy and want to create a class that is identical to another columnwise, but be backed by a different table, e.g.:
Base = declarative_base()
class Foo(Base):
__tablename__ = 'foo'
id = Column(Integer, primary_key=True)
a = Column(Text)
b = Column(Text)
class Bar(Base):
__tablename__ = 'bar'
id = Column(Integer, primary_key=True)
a = Column(Text)
b = Column(Text)
In this example, I'd like to create the class Bar in such a way that any change to the column set of Foo is reflected in Bar. There is no relationship between this data so I don't think inheritance is the right way to go.
Does this make sense?
Use a common mixin for both models:
class CommonModel(Base):
__abstract__ = True
id = Column(Integer, primary_key=True)
a = Column(Text)
b = Column(Text)
class Foo(CommonModel):
__tablename__ = 'foo'
class Bar(CommonModel):
__tablename__ = 'bar'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With