Python SQLAlchemy-无继承的多类型数组

Python SQLAlchemy-无继承的多类型数组,python,inheritance,python-3.x,sqlalchemy,Python,Inheritance,Python 3.x,Sqlalchemy,我有一套像这样的sqlalchemy模型 class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) username = Column(String) first_name = Column(String) last_name = Column(String) email = Column(String) user_group = Tabl

我有一套像这样的sqlalchemy模型

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    username = Column(String)
    first_name = Column(String)
    last_name = Column(String)
    email = Column(String)


user_group = Table('user_group', Base.metadata,
                   Column('user_id', Integer, ForeignKey('users.id')),
                   Column('group_id', Integer, ForeignKey('groups.id'))
                   )


class Group(Base):
    __tablename__ = 'groups'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    members = relationship("User", secondary=user_group)
我想创建第三个模型,它可以与
用户
类型建立1对多关系。所以我想最终得到一个可以像这样使用的东西

class Contact(Base):
    __tablename__ = 'contacts'

    id = Column(Integer, primary_key=True)
    type_id = Column(Integer)  # the id of the group or user row which it refers to.
    type = Column(String)  # 'group' or 'user'

class Thing(Base):
    __tablename__ = 'things'
    relationship('Contact')  # Array of Contacts
在哪里

我知道这可以通过继承来实现,但是还有其他选择吗?我宁愿不必对我的基本用户和组模型进行子类化

提前谢谢

阅读文档的一节。
您的描述与

thing = Thing()
thing.contacts[0].type # returns 'group' or 'user'
thing.contacts[0].contact  # returns the actual User or Group object