一尘不染

Android Room FOREIGN KEY约束失败(代码787)

sql

我想在AndroidRoom中用两个外键创建一个数据库。每次我尝试在数据库中插入曲目时,程序都会崩溃,并说“外键costraint失败(代码787)”。也许你们中的某人知道原因,可以为我提供帮助。

@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

道道

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”和“ Playlist”也是数据库中的表。

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

阅读 178

收藏
2021-03-10

共1个答案

一尘不染

你需要有表中的行Kategorie,并Playlist用相同的外键为具有Track要插入。

2021-03-10