简体   繁体   English

Android房间外键约束失败787。文件夹系统

[英]Android room foreign key constraint failed 787. Folder system

How can I realize folder system for db in Room? 如何在Room中实现数据库的文件夹系统? I want create folders and store in this some data. 我想创建文件夹并在其中存储一些数据。 Nested folders is not necessary. 不需要嵌套文件夹。

Before I use table with data (webSites) and folders table, then I create 3rd table WebSitesWithFolders, where I storing webSites_id and folders_id. 在将表与数据(webSites)和文件夹表一起使用之前,然后创建第三个表WebSitesWithFolders,在其中存储webSites_id和folder_id。 But when I update or delete field in users table all working good, but if I delete field from Folder, then I getting error foreign KEY constraint failed (code 787). 但是,当我更新或删除用户表中的字段时,它们都正常工作,但是如果我从“文件夹”中删除字段,那么我会得到错误外键约束失败(代码787)。

WebSites Entity 网站实体

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

Folders Entity 文件夹实体

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

In my experience you get the 787 foreign key constraint failed in Room when some ids that you declared in the relationship are missing. 以我的经验,当您在关系中声明的某些ID丢失时,您会在Room中获得787外键约束失败。 My mistake was that I was trying to add an object in a third table (for a many to many relationship) before adding the object in one of the parent tables. 我的错误是,我试图在第三个表中添加一个对象(用于多对多关系),然后再将该对象添加到一个父表中。

To wrap up, make sure that on delete you're not remaining in an inconsistent state (websites_with_folders table has ids that are no longer present in the website or folder tables). 最后,请确保在删除时您不会保持不一致的状态(websites_with_folders表的ID在网站或文件夹表中不再存在)。 Having cascade property on delete should help you with that, but I would double check what's happening there with a test. 在delete上具有层叠属性应该可以帮助您,但是我会通过测试再次检查发生了什么。 Also, I suggest that instead of using an "id" primary key for the websites_with_folders you should use multiple primary keys consisting of webSitesId and foldersId, like this: 另外,我建议不要对website_with_folders使用“ id”主键,而应使用由webSitesId和folderId组成的多个主键,如下所示:

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

Hope this solves your problem! 希望这能解决您的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM