chatzich
chatzich

Reputation: 1133

How can I inherit a class which inherits db.Model

I try to create another sql table using sqlalchemy by combining with a flask application

I have a class Table1 db.model which has some functionality and I want to use it's functionality by creating another Table2 class-table with some extra columns (which inherits the existing one), unfortunately when I tried to do this the automated form mechanism of SQLAlchemy did not create the table as it did with other classes which inherits db.model

db = SQLAlchemy(app)

class Table1(db.Model):
   __tablename__ = "table1"

   id                   = db.Column(db.String(36),  primary_key=True)

class Table2(Table1):
   __tablename__ = "table2"

   id                   = db.Column(db.String(36),  primary_key=True)
   extra_column         = db.Column(db.String(36),  primary_key=True)

Upvotes: 0

Views: 585

Answers (1)

chatzich
chatzich

Reputation: 1133

You have to add __abstract__ = True to Table1 BUT Table1 will not be created by SQLAlchemy ORM system

      db = SQLAlchemy(app)



      class Table1(db.Model):
           __tablename__ = "table1"
           __abstract__ = True

           id                   = db.Column(db.String(36),  primary_key=True)

        class Table2(Table1):
           __tablename__ = "table2"

           id                   = db.Column(db.String(36),  primary_key=True)
           extra_column         = db.Column(db.String(36),  primary_key=True)

Upvotes: 1

Related Questions