ActiveAndroid 与 Cascade 的一对多关系

ActiveAndroid One-To-Many relationship with Cascade

我有点困惑如何用 ActiveAndroid 创建 Model 来让两个 table 与 Cascade 条件 onDelete 和 codn 相关' 找到任何 good/clear 示例以供学习。 所以我有这个 table :

    @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 中列出它:

@Table(name = "Equipment")
public class Equipment {

    @Column(name = "Title")
    String Title;

    @Column(name = "Checklists")
    List<CheckList> Checklists;

}

我可能还有另一个 table,其中包含 Equipment 列表,我需要像上面那样关联它们。

我想要的是,当我从 Equipment 中删除一条记录时,我需要删除 List<CheckList> Checklists; 中与此 Equipment 相关的所有记录。我知道我可以进行查询等等,但我需要知道是否有更好的方法和正确的方法来做到这一点?

请详细解释(如何创建关系和稍后查询)并显示与我的 tables 相关的示例。

您需要设置具有外键级联关系的表。

@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;

}

参考资料:

  1. Reference article
  2. Official docs with basics on how to set up relationships & models
  3. Closed issue that confirms that CASCADE DELETE works