Android Room On删除级联不起作用

时间:2018-12-16 00:14:49

标签: android database android-room cascade

我知道有很多关于此类主题的文章,但是它们都涉及SQLite ...(我的意思是关于Room)。

我在“卡”表和“甲板”表之间存在一对多关系。因此,一张纸牌有1张牌,而一张纸牌有很多牌。插入和删除对于1行都可以,但是当我删除一副卡片组时,我想删除它:

我的代码:

(Card.class)

@Entity(tableName = "cards")
public class Card{

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    public int id;

    @ColumnInfo(name = "face1")
    public String face1;

    @ColumnInfo(name = "face2")
    public String face2;

    @ColumnInfo(name = "id_deck")
    @ForeignKey(entity = Deck.class, parentColumns = "id", childColumns = 
    "id_deck", onDelete = CASCADE)
    public int id_deck;
}

Deck.class:

@Entity(tableName = "decks")
public class Deck {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    public int id;

    @ColumnInfo(name = "nom")
    public String nom;

    public Deck(String nom){
        this.nom = nom;
    }

}

我做错什么了吗?当我删除卡片组时,卡片仍然存在。

1 个答案:

答案 0 :(得分:1)

您应在@Entity批注中定义外键。

@Entity(tableName = "cards", foreignKeys = @ForeignKey(entity = Deck.class, parentColumns = "id", childColumns = "id_deck", onDelete = CASCADE))