【问题标题】:python-storm orm many-to-manypython-storm orm 多对多
【发布时间】:2011-12-11 11:36:21
【问题描述】:

我正在使用 python-storm 作为 orm。多对多参考集让我头疼:(

这些是相关的对象:

class Author(object):
    __storm_table__ = "author"
    id = Int(primary=True)
    name = Unicode()
    institution_id = Int()
    institution = Reference(institution_id, Institution.id)

    def __init__(self, name):
        self.name = name


class Paper(object):
    __storm_table__ = "paper"
    id = Int(primary=True)
    name = Unicode()
    conference_id = Int()
    conference = Reference(conference_id, Conference.id)

    def __init__(self, name):
        self.name = name

class AuthorPapers(object):
    __storm_table__ = "authorpapers"
    __storm_primary__ = "author_id", "paper_id"
    author_id = Int()
    paper_id = Int()

各自的sqlite表是这样的

store.execute("CREATE TABLE if not exists author (id INTEGER PRIMARY KEY, name VARCHAR, institution_id INTEGER, FOREIGN KEY (institution_id) REFERENCES institution(id))")

store.execute("CREATE TABLE if not exists paper (id INTEGER PRIMARY KEY, name VARCHAR, conference_id INTEGER, FOREIGN KEY (conference_id) REFERENCES conference(id))")

store.execute("CREATE TABLE if not exists authorpapers (author_id INTEGER, paper_id INTEGER, PRIMARY KEY (author_id, paper_id))")

现在说如果有两个作者合作写了一篇论文

a = Author(u"Steve Rogers")
b = Author(u"Captain America")

还有一篇论文

p6 = Paper(u"Bunga Bunga")

所以现在我想将两位作者与论文联系起来使用

Author.papers = ReferenceSet(Author.id, AuthorPapers.author_id, Paper.id, AuthorPapers.paper_id)

然后这样做

a.papers.add(p6)
b.papers.add(p6)

顺便说一句,它说它应该在风暴教程中工作......但我明白了

  File "/usr/lib64/python2.7/site-packages/storm/references.py", line 376, in add
    self._relation2.link(remote, link, True)
  File "/usr/lib64/python2.7/site-packages/storm/references.py", line 624, in link
    pairs = zip(self._get_local_columns(local.__class__),
  File "/usr/lib64/python2.7/site-packages/storm/references.py", line 870, in _get_local_columns
    for prop in self.local_key)
  File "/usr/lib64/python2.7/site-packages/storm/references.py", line 870, in <genexpr>
    for prop in self.local_key)
  File "/usr/lib64/python2.7/site-packages/storm/properties.py", line 53, in __get__
    return self._get_column(cls)
  File "/usr/lib64/python2.7/site-packages/storm/properties.py", line 97, in _get_column
    attr = self._detect_attr_name(cls)
  File "/usr/lib64/python2.7/site-packages/storm/properties.py", line 82, in _detect_attr_name
    raise RuntimeError("Property used in an unknown class")
RuntimeError: Property used in an unknown class

而我现在真的无法理解这一点。

【问题讨论】:

    标签: python orm storm-orm


    【解决方案1】:

    我对@9​​87654322@ 不是很熟悉,但看看documentation 的例子,似乎只是与ReferenceSet 的参数传递顺序有关的问题。我试着用这个:

    Author.papers = ReferenceSet(Author.id, AuthorPapers.author_id, AuthorPapers.paper_id, Paper.id)
    

    而不是这个:

    Author.papers = ReferenceSet(Author.id, AuthorPapers.author_id, Paper.id, AuthorPapers.paper_id)
    

    并且没有引发异常。

    【讨论】:

    • 太棒了!比你好多了。似乎我最近一直在盯着代码;)
    猜你喜欢
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多