我对如何使用Model
创建ActiveAndroid
并使其具有两个与Cascade
条件onDelete
相关的表感到困惑,并且找不到任何好/清晰的方法值得学习的例子。
所以我有这张桌子:
@Table(name = "CheckList")
public class CheckList {
@Column(name = "Title")
String Title;
@Column(name = "After")
Integer After;
@Column(name = "Before")
Integer Before;
@Column(name = "Enabled")
Boolean Enabled;
@Column(name = "Info")
String Info;
}
并且我需要在此表中列出它:
@Table(name = "Equipment")
public class Equipment {
@Column(name = "Title")
String Title;
@Column(name = "Checklists")
List<CheckList> Checklists;
}
我可能还有另一个带有Equipment
列表的表,我需要像上面那样将它们关联起来。
我想要的是,当我从Equipment
删除记录时,我也需要删除List<CheckList> Checklists;
中与此Equipment
相关的所有记录。我知道我可以进行查询等等,但是我需要知道是否有更好的方法和正确的方法来做到这一点?
请详细说明(如何创建关系和稍后查询),并显示与我的表相关的示例。
答案 0 :(得分:0)
您需要使用外键级联关系设置表。
@Table(name = "Equipment")
public class Equipment {
@Column(name = "Title")
String Title;
// This method is optional & does not affect the foreign key creation.
public List<CheckList> items() {
return getMany(CheckList.class, "Equipment");
}
}
@Table(name = "CheckList")
public class CheckList {
@Column(name = "Title")
String Title;
@Column(name = "After")
Integer After;
@Column(name = "Before")
Integer Before;
@Column(name = "Enabled")
Boolean Enabled;
@Column(name = "Info")
String Info;
//This establishes a relationship between Checklist and Equipment, Any update or delete operations done on Equipment table gets cascaded to the corresponding rows in the Checklist table.
@Column(name = "Equipment", onUpdate = ForeignKeyAction.CASCADE, onDelete = ForeignKeyAction.CASCADE)
Equipment equipment;
}