我有一个基本关系定义如下: db = Database('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()
删除旧条目,然后将新条目添加到趋势集中。
在此操作后生成的趋势表中,我添加了项目,但以前的项目(从集合中清除)没有被删除,它们留在数据库中,“位置”字段为空(我猜它们被取消引用)。
我应该如何执行上述操作来读取孤立项?