在 sqlalchemy 中使用版本控制和多级继承时出现问题

发布于 2024-11-15 16:04:25 字数 2134 浏览 2 评论 0原文

我正在尝试使用 sqlalchemy 网站上描述的版本控制配方(http: //www.sqlalchemy.org/docs/orm/examples.html#versioned-objects)以及多级继承模型(连接表继承)

以下是我的声明性语句:

class Sample(Base):
    __metaclass__ = VersionedMeta
    __tablename__ = 'sample'
    __table_args__ = {'schema': 'test'}

    id = Column(Integer, primary_key=True)
    discriminator = Column('type', String(50))
    token = Column(String(128), nullable=False)
    source_sample_id = Column(Integer, ForeignKey('test.sample.id'))

    children = relationship("Sample", backref=backref('source_sample', remote_side=id), single_parent=True)

    __mapper_args__ = {'polymorphic_on': discriminator, 'polymorphic_identity':'sample'}

    def __init__(self, token, source_sample_id=None):
        self.token = token
        self.source_sample_id = source_sample_id

class Tissue(Sample):
    __metaclass__ = VersionedMeta
    __tablename__ = 'tissue'
    __mapper_args__ = {'polymorphic_identity': 'tissue'}
    __table_args__ = {'schema': 'test'}

    id = Column(Integer, ForeignKey('test.sample.id'), primary_key=True)
    concentration = Column(String(32))

    def __init__(self, token, concentration, source_sample_id=None):
        super(Sample, self).__init__(token, source_sample_id)
        self.concentration = concentration

class LeukemicTissue(Tissue):
    __metaclass__ = VersionedMeta
    __tablename__ = 'leukemic_tissue'
    __mapper_args__ = {'polymorphic_identity': 'leukemic_tissue'}
    __table_args__ = {'schema': 'test'}

    id = Column(Integer, ForeignKey('test.tissue.id'), primary_key=True)
    leukemia = Column(String)

    def __init__(self, token, concentration, leukemia, source_sample_id=None):
        super(Tissue, self).__init__(token, concentration, source_sample_id)
        self.leukemia = leukemia 

每当我尝试“create_all()”我收到以下错误: sqlalchemy.exc.ArgumentError:找不到“tissue_history”和“leucegene_tissue_history”之间的任何外键关系。

单级继承效果很好(即:如果我停在“Tissue”并且不声明“LeukemicTissue”),但我确实需要一个多级继承方案才能工作。

任何人都可以给我任何指示吗?

谢谢 !!

I'm trying to use the versioning recipe described on the sqlalchemy website (http://www.sqlalchemy.org/docs/orm/examples.html#versioned-objects) along with a multi-level inheritance model (Joined-Table inheritance)

Here are my declarative statements:

class Sample(Base):
    __metaclass__ = VersionedMeta
    __tablename__ = 'sample'
    __table_args__ = {'schema': 'test'}

    id = Column(Integer, primary_key=True)
    discriminator = Column('type', String(50))
    token = Column(String(128), nullable=False)
    source_sample_id = Column(Integer, ForeignKey('test.sample.id'))

    children = relationship("Sample", backref=backref('source_sample', remote_side=id), single_parent=True)

    __mapper_args__ = {'polymorphic_on': discriminator, 'polymorphic_identity':'sample'}

    def __init__(self, token, source_sample_id=None):
        self.token = token
        self.source_sample_id = source_sample_id

class Tissue(Sample):
    __metaclass__ = VersionedMeta
    __tablename__ = 'tissue'
    __mapper_args__ = {'polymorphic_identity': 'tissue'}
    __table_args__ = {'schema': 'test'}

    id = Column(Integer, ForeignKey('test.sample.id'), primary_key=True)
    concentration = Column(String(32))

    def __init__(self, token, concentration, source_sample_id=None):
        super(Sample, self).__init__(token, source_sample_id)
        self.concentration = concentration

class LeukemicTissue(Tissue):
    __metaclass__ = VersionedMeta
    __tablename__ = 'leukemic_tissue'
    __mapper_args__ = {'polymorphic_identity': 'leukemic_tissue'}
    __table_args__ = {'schema': 'test'}

    id = Column(Integer, ForeignKey('test.tissue.id'), primary_key=True)
    leukemia = Column(String)

    def __init__(self, token, concentration, leukemia, source_sample_id=None):
        super(Tissue, self).__init__(token, concentration, source_sample_id)
        self.leukemia = leukemia 

Whenever I try to "create_all()" I get the following error:
sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships between 'tissue_history' and 'leucegene_tissue_history'.

Single level-inheritance works beautifully (ie: if I stop at "Tissue" and don't declare the "LeukemicTissue") but I really need a multi-level inheritance scheme to work..

Can anyone give me any pointers ?

Thanks !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慕巷 2024-11-22 16:04:25

好吧,看起来 zzzeek 刚刚修复了您在此 变更集。因此,只需更新您这边的文件就可以了。

注意:另外,请注意您似乎滥用了 super(...) :您应该使用类本身作为第一个参数,而不是基类:

class LeukemicTissue(Tissue):
    # ...
    def __init__(self, token, concentration, leukemia, source_sample_id=None):
        #super(Tissue, self).__init__(token, concentration, source_sample_id) # ERROR
        super(LeukemicTissue, self).__init__(token, concentration, source_sample_id) # GOOD

Well, it looks like zzzeek just fixed the bug you described in this changeset. So just update the file on your side and it should work.

Note: Also, please note that you seem to be misusing super(...) in your code: you should use the class itself as the first parameter, not the base class:

class LeukemicTissue(Tissue):
    # ...
    def __init__(self, token, concentration, leukemia, source_sample_id=None):
        #super(Tissue, self).__init__(token, concentration, source_sample_id) # ERROR
        super(LeukemicTissue, self).__init__(token, concentration, source_sample_id) # GOOD
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文