I have two very similar tables in my database that I want to model using SQLAlchemy ORM. One is used for production and the other for QA so they are exactly the same except for tablename. I'm just starting to learn classes and class methods and I want to make my code DRY so I don't want to repeat the same column names and methods definitions for the QA tables.
What I have right now (very simplified):
class Versions(db.Model):
__tablename__ = 'versions'
ver_key = Column('version_key', Integer, primary_key=True)
market = Column('market', String(100))
year = Column('year', Integer)
def __init__(self, market, year):
self.market = market
self.year = year
@classmethod
def create(cls, market, year):
new_rec = cls(market, year)
db.session.add(new_rec)
db.session.commit()
return new_rec.ver_key
class VersionsQA(Versions):
__tablename__ = 'qa_versions'
Unfortunately this doesn't work as intended as using VersionsQA.create(market, year) creates records in the Versions table instead of VersionsQA.
Can you please help me understand how this should be written properly?
PS @classmethod is needed because it does a lot of other things which I don't want to write here