1

我尝试将 SqlAlchemy 单表继承示例移植到 Camelot

class Person(Entity):
    __tablename__ = 'people'
    id = Column(sqlalchemy.Integer, primary_key=True)
    discriminator = Column('type', sqlalchemy.String(50))
    __mapper_args__ = {'polymorphic_on': discriminator}

    class Admin(EntityAdmin):
        verbose_name = 'Person'
        list_display = ['id','discriminator']

class Engineer(Person):
    __mapper_args__ = {'polymorphic_identity': 'engineer'}
    primary_language = Column(sqlalchemy.String(50))

    class Admin(EntityAdmin):
        verbose_name = 'HR'
        list_display = ['id','discriminator']

但这会产生以下错误:

  Traceback (most recent call last):
    File "D:/Programming/test/main.py", line 41, in <module>
      start_application()
    File "D:/Programming/test/main.py", line 37, in start_application
      from testing.application_admin import MyApplicationAdmin
    File "D:\Programming\test\testing\application_admin.py", line 6, in <module>
      from testing.model import Engineer, Person
    File "D:\Programming\test\testing\model.py", line 17, in <module>
      class Engineer(Person):
    File "C:\Python27\lib\site-packages\camelot\core\orm.py", line 334, in __init__
      super( EntityMeta, cls ).__init__( classname, bases, dict_ )
    File "C:\Python27\lib\site-packages\sqlalchemy\ext\declarative.py", line 1343, in __init__
      _as_declarative(cls, classname, cls.__dict__)
    File "C:\Python27\lib\site-packages\sqlalchemy\ext\declarative.py", line 1336, in _as_declarative
      **mapper_args)
    File "C:\Python27\lib\site-packages\sqlalchemy\orm\__init__.py", line 1129, in mapper
      return Mapper(class_, local_table, *args, **params)
    File "C:\Python27\lib\site-packages\sqlalchemy\orm\mapper.py", line 197, in __init__
      self._configure_inheritance()
    File "C:\Python27\lib\site-packages\sqlalchemy\orm\mapper.py", line 473, in _configure_inheritance
      self.local_table)
    File "C:\Python27\lib\site-packages\sqlalchemy\sql\util.py", line 303, in join_condition
      "between '%s' and '%s'.%s" % (a.description, b.description, hint))
  sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships between 'people' and 'engineer'.

我在做一些不是 Camelot 方式的事情吗?为模型实现继承的最佳方法是什么?我在 Camelot 的文档中找不到任何内容。

编辑:

我尝试将以下内容添加到工程师类

__tablename__ = 'people'
__table_args__ = {'extend_existing': True}

但是现在在我尝试选择 Person 或 Engineer 表之后,或者如果我尝试添加它们中的任何一个,我会得到:

C:\Python27\lib\site-packages\sqlalchemy\sql\expression.py:2276: SAWarning: Column u'people_id' on table <sqlalchemy.sql.expression.Select at 0x343fb90; Select object> being replaced by another column with the same key.  Consider use_labels for select() statements.
self[column.key] = column

有小费吗?

4

1 回答 1

3

在你的 Engineer 类中,__tablename__需要设置为 None,否则 Camelot 会分配一个默认的表名,SQLAlchemy 会假设多表继承,因此需要外键关系。

于 2013-04-12T19:05:24.370 回答