1

我是 SQLAlchmey 的新手,我正在尝试实现以下对象/数据库表结构(也使用 alembic):

class BaseConfig(Base):
 pk = Column(Integer, primary_key=True)
 name = Column(Unicode(150), nullable=False, unique=True)
 ...
 # Lots of other general columns, such as description, display_name, creation time etc.

我希望所有其他配置类都从中继承预定义的列:

class A(BaseConfig):
  __tablename__ = "A"
  column1 = Column...
  column2 = Column...

class B(BaseConfig):
  __tablename__ = "B"
  column1 = Column...
  column2 = Column...

BaseConfig表不是真正的表,它只是一个保存公共列的类除此之外 - A 和 B 之间没有任何关系,也不需要共享 pk 等。似乎使用“ polymorphic_union ”在这里也无关紧要。

通过尝试运行 alembic autogenerate 我得到了 BaseConfig 没有表映射类的错误 - 这是真的,我真的没有理由将“多态联合”选项添加到 BaseConfig,因为这个类是通用的. 有什么建议么?(在 Django with south 中,这是开箱即用的,但在这里似乎不容易支持这种行为)。

谢谢,李

4

1 回答 1

6

要么使用 MixIns(阅读文档中的Mixing in Columns部分),其中您BaseConfig没有子类和实际表子类并且:BaseBaseBaseConfig

class BaseConfig(object):
    # ...

class MyModel(BaseConfig, Base):
    # ...

或者简单地使用__abstract__

class BaseConfig(Base):
    __abstract__ = True
于 2014-10-07T09:37:39.113 回答