简体   繁体   中英

SQLAlchemy - model simple inheritance

Is there anything wrong with inheritance in which child class is only used to present parent's values in a different way?

Example:

class Parent(db.Model):

    __tablename__ = u'parent'

    parent_entry_id = db.Column(db.Integer, primary_key=True)
    parent_entry_value = db.Column(db.BigInteger)

class Child(Parent):

   __tablename__ = u'child'

   @property
   def extra_value(self):
       return unicode(self.parent_entry_id) + unicode(self.parent_entry_value)

No new values will be added Child class, thus Joined Table, Single Table or Concrete Table Inheritance, as for me, is not needed.

If you're simply changing how you display the data from the class, I'm pretty sure you don't need a __tablename__ .

Additionally, though I don't know your exact problem domain, I would simply just add the property on the original class. You could argue that you're adding some extra behavior to your original class, but that seems like a bit of a flimsy argument in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM