Итак, у меня есть база данных с таблицей Deck, таблицей Card и таблицей DeckCard, потому что между этими двумя типами есть отношение multi-multi ... когда я пытаюсь удалить колоду, я удаляю строку DeckCard, где идентификатор совпадает, и я установил внешний ключ onDelete = CASCADE, но он не удаляет колоду какие-либо предложения? Это хранилище для моего различения
public class DeckCardRepository {
private DeckWithCardsDao mDeckWithCardsDao;
public void delete(Deck deck){
DecksDatabase.databaseWriteExecutor.execute(() ->
mDeckWithCardsDao.delete(deck.getId()));
}
}
Класс колоды
@Entity(tableName = "decks")
public class Deck implements Serializable {
@PrimaryKey
private long id;
private String keyforgeId;
private String name;
@TypeConverters({ExpansionTypeConverter.class})
private Expansion expansion;
private int creatureCount;
private int actionCount;
private int artifactCount;
private int upgradeCount;
private int sasRating;
private int powerLevel;
private int chains;
private int wins;
private int losses;
private int totalPower;
private int totalArmor;
private int localWins;
private int localLosses;
}
Класс карты
Entity(tableName = "cards")
public class Card implements Serializable {
@PrimaryKey
@NonNull
private String id;
private String card_title;
private String card_type;
@TypeConverters({HouseArrayTypeConverter.class})
private House house;
private String card_text;
private int amber;
private String front_image;
}
Класс колоды карт
Entity(tableName = "cards_deck_join",
primaryKeys = {"cardId", "deckId"},
foreignKeys = {
@ForeignKey(
entity = Card.class,
parentColumns = "id",
childColumns = "cardId"),
@ForeignKey(onDelete = CASCADE,
entity = Deck.class,
parentColumns = "id",
childColumns = "deckId"),
})
public class CardsDeckRef {
@NonNull
private String cardId;
private long deckId;
private int count;
public CardsDeckRef(String cardId, long deckId, int count) {
this.cardId = cardId;
this.deckId = deckId;
this.count = count;
}
}
MyDao
@Dao
public interface DeckWithCardsDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
void add(CardsDeckRef cardsDeckRef);
@Transaction
@Query("SELECT * FROM cards INNER JOIN cards_deck_join" +
" ON cards.id=cards_deck_join.cardId WHERE cards_deck_join.deckId =:deckId")
LiveData<List<Card>> getCardsForDeck(final long deckId);
@Query("DELETE FROM cards_deck_join WHERE cards_deck_join.deckId=:deckId ")
void delete(final long deckId);
}