本文介绍了Android Room FOREIGN KEY约束失败(代码787)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用两个外键在Android Room中创建一个数据库.每次我尝试在数据库中插入曲目时,程序都会崩溃,并说外键costraint失败(代码787)".也许你们中的某人知道原因,并且可以帮助我.
I'm tying to create a database in Android Room with two foreign keys. Every time I try to insert a track into the database the program chrashes and says that the "foreign key costraint failed(code 787)". Maybe someone of you knows why and can help me.
@Entity(foreignKeys = {@ForeignKey(
entity = Kategorie.class,
childColumns = "kategorieFremdschluessel",
parentColumns = "kategorieID",
onUpdate = ForeignKey.CASCADE,
onDelete = ForeignKey.CASCADE
),
@ForeignKey(
entity = Playlist.class,
childColumns = "playlistFremdschluessel",
parentColumns = "uuid",
onUpdate = ForeignKey.CASCADE,
onDelete = ForeignKey.CASCADE
)
})
public class Track {
@PrimaryKey(autoGenerate = true)
private int uid;
private String trackTitel;
private String playlistName;
private String jsonObjectString;
private int kategorieFremdschluessel;
private int playlistFremdschluessel;
@Ignore
public Track(String trackTitel, String playlistName, String jsonObjectString) {
this.trackTitel = trackTitel;
this.playlistName = playlistName;
this.jsonObjectString = jsonObjectString;
}
public Track(String trackTitel, String jsonObjectString) {
this.trackTitel = trackTitel;
this.jsonObjectString = jsonObjectString;
}
//Getter and Setter
@Dao
TrackDao
public interface TrackDao {
@Query("SELECT * FROM Track WHERE playlistName LIKE :playlist")
List<Track> getAllTracks(String playlist);
@Query("SELECT * FROM Track WHERE kategorieFremdschluessel = :kategorieFremdschluessel")
List<Track> loadAllKategorieTracks(int kategorieFremdschluessel);
@Query("SELECT * FROM Track WHERE playlistFremdschluessel = :playlistFremdschluessel")
List<Track> loadAllPlaylistTracks(int playlistFremdschluessel);
@Insert
void insertAll(List<Track> trackList);
@Insert
void insertOne(Track track);
@Update
void updateOne(Track track);
@Delete
void delete(Track track);
}
类别"和播放列表"也是数据库中的表.
"Kategorie" and "Playlist" are also tables in the database.
@Entity
public class Playlist{
@PrimaryKey(autoGenerate = true)
private int uuid;
@ColumnInfo(name = "name")
private String name;
类别
@Entity
public class Kategorie {
@PrimaryKey(autoGenerate = true)
private int kategorieID;
@ColumnInfo(name = "name")
private String name;
推荐答案
您需要在表Kategorie
和Playlist
中的行具有相同的外键,就像您要插入的Track
一样.
You need to have rows in tables Kategorie
and Playlist
with a same foreign key as has Track
that you are inserting.
这篇关于Android Room FOREIGN KEY约束失败(代码787)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!