Android机房外键约束失败787.文件夹系统

时间:2018-04-17 13:39:42

标签: android foreign-keys many-to-many relation android-room

如何在房间中实现db的文件夹系统?我想创建文件夹并存储一些数据。不需要嵌套文件夹。

在我将表与数据(webSites)和文件夹表一起使用之前,我创建了第3个表WebSitesWithFolders,其中我存储了webSites_id和folders_id。但是当我更新或删除用户表中的字段时一切正常,但是如果我从Folder中删除字段,那么我得到错误的外键约束失败(代码787)。

网站实体

@Entity
public class WebSites {

@PrimaryKey(autoGenerate = true)
private int id;

@ColumnInfo()
private String website_head;

@ColumnInfo()
private String website_url;

@ColumnInfo()
private String img_src;


public WebSites(@NonNull String website_head, String website_url, String img_src) {
    this.website_head = website_head;
    this.website_url = website_url;
    this.img_src = img_src;
}


public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}


public String getWebsite_head() {
    return website_head;
}

public void setWebsite_head(String website_head) {
    this.website_head = website_head;
}

public String getWebsite_url() {
    return website_url;
}

public void setWebsite_url(String website_url) {
    this.website_url= website_url;
}

public String getImg_src() {
    return img_src;
}

public void setImg_src(String img_src) {
    this.img_src = img_src;
}
}

文件夹实体

@Entity
public class Folders {


@PrimaryKey(autoGenerate = true)
private int id;

@ColumnInfo()
private String name;

public Folders(String name) {
    this.name = name;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

WebSitesWithFolders

@Entity(tableName = "websites_with_folders",
    foreignKeys = {
            @ForeignKey(entity = WebSites.class,
                        parentColumns = "id",
                        childColumns = "webSitesId",
                        onDelete = CASCADE,
                        onUpdate = CASCADE),
            @ForeignKey(entity = Folders.class,
                        parentColumns = "id",
                        childColumns = "foldersId",
                        onDelete = CASCADE,
                        onUpdate = CASCADE)
            })

public class WebSitesWithFolders {

@PrimaryKey(autoGenerate = true)
private int idd;

@ColumnInfo()
private int webSitesId;

@ColumnInfo()
private int foldersId;

public WebSitesWithFolders(int webSitesId, int foldersId) {
    this.webSitesId = webSitesId;
    this.foldersId = foldersId;
}

public int getWebSitesId() {
    return webSitesId;
}

public void setWebSitesId(int webSitesId) {
    this.webSitesId = webSitesId;
}

public int getFoldersId() {
    return foldersId;
}

public void setFoldersId(int foldersId) {
    this.foldersId = foldersId;
}

public int getIdd() {
    return idd;
}

public void setIdd(int idd) {
    this.idd = idd;
}
}

1 个答案:

答案 0 :(得分:0)

根据我的经验,当您在关系中声明的某些ID丢失时,您在Room中遇到了787外键约束失败。我的错误是我试图在第三个表中添加一个对象(用于多对多关系),然后再将该对象添加到父表之一中。

最后,请确保在删除时您不会保持不一致的状态(websites_with_folders表的ID在网站或文件夹表中不再存在)。在delete上具有层叠属性应该可以帮助您,但是我会通过测试再次检查发生了什么。另外,我建议不要对website_with_folders使用“ id”主键,而应使用由webSitesId和folderId组成的多个主键,如下所示:

@Entity(tableName = "websites_with_folders", 
        primaryKeys ={webSitesId ,foldersId}

希望这可以解决您的问题!