清除定义一对多关系的集合时,PonyORM孤立的项目

时间:2015-06-03 09:53:13

标签: ponyorm

我的基本关系定义如下:    db =数据库(' sqlite',' test_db.sqlite',create_db = True)

class WOEID(db.Entity):
    woeid      = PrimaryKey(int)
    iso        = Optional(str)
    name       = Required(str)
    language   = Optional(str)
    place_type = Required(str)
    parent_id  = Required(int)
    trends     = Set('Trend')
    ancestry   = Optional(str)

class Trend(db.Entity):
    woeid                = Required(int)
    events               = Optional(str)
    name                 = Required(str)
    promoted_content     = Optional(str)
    query                = Required(str)
    url                  = Required(str)
    location             = Optional(WOEID)

db.generate_mapping(create_tables=True) 

现在,我在用@db_session修饰的函数中向WOEID.trends添加了一些项目。这按预期工作。 现在我尝试通过首先使用

读取对象来更新WOEID.trends
 location = WOEID.get(woeid = some_woeid)

稍后我发出

location.trends.clear()

删除旧条目,然后向趋势集添加新项目。

在此操作之后生成的趋势表中,我添加了项目,但之前的项目(从集合中清除)不会被删除,它们将保留在数据库中,并且位置为'字段为空(我猜它们被解除引用)。

如何执行上述操作以阅读孤立的项目?

1 个答案:

答案 0 :(得分:3)

PonyORM中有两种一对多关系。第一种关系是关系的一端是Set而关系的另一端是Required。在这种情况下,当您从集合中删除项目时,此项目将被删除。例如,我们可以通过以下方式定义两个实体ArticleComment

class Article(db.Entity):
    author = Required(User)
    text = Required(str)
    comments = Set('Comment')

class Comment(db.Entity):
    author = Required(User)
    text = Required(str)
    article = Required(Article)

在这种情况下,当您执行article.comments.clear()时,所有评论都将被删除,因为Comment.article属性是必需的,如果没有文章,评论就不会存在。

另一种关系是将Comment.article属性定义为Optional

class Comment(db.Entity):
    author = Required(User)
    text = Required(str)
    article = Optional(Article)

在这种情况下,评论可以在没有任何文章的情况下存在,当您从Article.comments集合中删除评论时,它仍保留在数据库中,但Comment.article属性值设置为{{1} }。

您可以通过执行以下查询找到孤立的项目:

NULL

或等同于

select(c for c in Comment if c.article is None)

在某些情况下,可能需要将属性定义为Comment.select(lambda c: c.article is None) ,但在从集合中删除项目时执行级联删除。为此,您可以为Optional属性指定cascade_delete选项:

Set

然后,如果您执行class Article(db.Entity): author = Required(User) text = Required(str) comments = Set('Comment', cascade_delete=True) class Comment(db.Entity): author = Required(User) text = Required(str) article = Optional(Article) ,则将从数据库中删除所有已删除的注释。

相关问题