Prasadnsr
Prasadnsr

Reputation: 481

SQLAlchemy ORM - adding two class attributes to a single table

I have independent class-A which is inherited in an another class-B. Class-A doesn't have a table of its own, it shares the columns in the Class-B's table. If I have to map these classes using a mapper, how will I write a mapper. Example:-

class Class_A(object):

    def __init__(self,name, value):
        self.name = name
        self.value = value

class Class_B(Class_A):

    def __init__(self, id, amt):
        self.id = id
        self.amt = amt

Table_B = Table('tableB', metadata,

    Column('id', Integer)
    Column('name', String(20))
    Column('value', String(20))
    Column('amt', Integer)
)

Please help me.

Upvotes: 2

Views: 398

Answers (1)

khoxsey
khoxsey

Reputation: 1403

The SA documentation for the ORM discusses a number of different use cases for handling object inheritance across a relational table structure. It looks like your use case is the Single Table Inheritance model. There are good examples listed that resemble your example closely and should be easy to put to work.

Upvotes: 1

Related Questions