gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:34:50 25 4
gpt4 key购买 nike

我想在 Android Room 中使用两个外键创建一个数据库。每次我尝试将轨道插入数据库时​​,程序都会崩溃并提示“外键约束失败(代码 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);
}

“类别”和“播放列表”也是数据库中的表。

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

最佳答案

出现此错误还有另一个原因,它占用了我大量时间,因为互联网上可用的所有可能解决方案对我来说似乎都是徒劳的。

如果您使用父表的 rowId 作为 parentColumn 键,则不能使用外键约束。

根据 SQLite documentation on Foreign Keys :

The parent key is the column or set of columns in the parent table that the foreign key constraint refers to. This is normally, but not always, the primary key of the parent table. The parent key must be a named column or columns in the parent table, not the rowid.

关于Android Room FOREIGN KEY 约束失败(代码 787),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50729537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com