【发布时间】:2014-10-10 12:55:34
【问题描述】:
我在 android 中使用 ormlite,并已成功创建表并使用 DAO 执行不同的操作。
但是如果从主表中删除主键,我一直在删除外键行。
我有两个名为 Parent 和 child 的表。父母有多个孩子,所以我将孩子与外键联系起来。
代码:
父表:
@DatabaseField(id = true)
private Integer id;
@ForeignCollectionField(eager = false)
private ForeignCollection<Child> childCollection;
子表:
@DatabaseField(id = true)
private Integer id;
@DatabaseField(foreign = true, foreignAutoRefresh = true, canBeNull = false,
index = true, columnDefinition = "INTEGER REFERENCES parent(id) ON DELETE CASCADE")
private Parent parent;
现在,如果我要删除特定 ID 的父行,那么这不会从子表中删除子行。
public void deleteById(Integer parentId) {
try {
Dao<Parent, Integer> parentDao = databaseHelper.getParentDao();
parentDao .deleteById(parentId);
} catch (SQLException e) {
e.printStackTrace();
}
}
请指导我,我做错了什么。我已经多次尝试和谷歌,但没有运气。
【问题讨论】:
标签: android database orm android-sqlite ormlite