我正在使用Room来设置包含3个表的数据库。 我正在尝试从“文章”表中为2个表(标题和多媒体)设置外键,但没有设置主键。
请参见下面的代码。我认为我正确使用了批注,但是它仍然给我以下错误:
错误:com.articlesregistry.data.Multimedia有一个外键(article_original_id),它引用com.articlesregistry.data.Article(original_id),但com.articlesregistry.data.Article在这些列和索引上都没有唯一索引列是其主键。 SQLite要求对引用的父列具有唯一的约束,因此您必须向具有(original_id)列的com.articlesregistry.data.Article添加唯一索引。
和类:
文章:
@Entity(tableName = "article",
indices={
@Index(value="uid"),
@Index(value="original_id")
})
public class Article {
@Ignore
ArrayList<Multimedia> multimediaArrayList;
@Ignore
ArrayList<Headline> headlineArrayList;
@Expose(deserialize = false)
@NonNull
@PrimaryKey(autoGenerate = true)
private int uid;
@Expose(deserialize = false)
@ColumnInfo(name = "favourite")
private Boolean favourite;
// Json - API
@SerializedName("web_url")
@ColumnInfo(name = "web_url")
private String web_url;
@SerializedName("snippet")
@ColumnInfo(name = "snippet")
private String snippet;
@SerializedName("source")
@ColumnInfo(name = "source")
private String source;
@SerializedName("pub_date")
@ColumnInfo(name = "pub_date")
private String pub_date;
@SerializedName("document_type")
@ColumnInfo(name = "document_type")
private String document_type;
@SerializedName("_id")
@NonNull
@ColumnInfo(name = "original_id")
private String original_id;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getWeb_url() {
return web_url;
}
public void setWeb_url(String web_url) {
this.web_url = web_url;
}
public String getSnippet() {
return snippet;
}
public void setSnippet(String snippet) {
this.snippet = snippet;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getPub_date() {
return pub_date;
}
public void setPub_date(String pub_date) {
this.pub_date = pub_date;
}
public String getDocument_type() {
return document_type;
}
public void setDocument_type(String document_type) {
this.document_type = document_type;
}
public Boolean getFavourite() {
return favourite;
}
public void setFavourite(Boolean favourite) {
this.favourite = favourite;
}
@NonNull
public String getOriginal_id() {
return original_id;
}
public void setOriginal_id(@NonNull String original_id) {
this.original_id = original_id;
}
public ArrayList<Multimedia> getMultimediaArrayList() {
return multimediaArrayList;
}
public void setMultimediaArrayList(ArrayList<Multimedia> multimediaArrayList) {
this.multimediaArrayList = multimediaArrayList;
}
public ArrayList<Headline> getHeadlineArrayList() {
return headlineArrayList;
}
public void setHeadlineArrayList(ArrayList<Headline> headlineArrayList) {
this.headlineArrayList = headlineArrayList;
}
}
标题:
@Entity(tableName = "headline",
foreignKeys = @ForeignKey(entity = Article.class,
parentColumns = "original_id",
childColumns = "article_original_id",
onDelete = CASCADE),
indices = {@Index("article_original_id")})
public class Headline {
@Expose(deserialize = false)
@NonNull
@PrimaryKey(autoGenerate = true)
private int uid;
@Expose(deserialize = false)
@NonNull
@ColumnInfo(name = "article_original_id")
private String article_original_id;
// Json - API
@SerializedName("main")
@ColumnInfo(name = "headline_main")
private String headline_main;
@SerializedName("kicker")
@ColumnInfo(name = "headline_content_kicker")
private String headline_content_kicker;
@SerializedName("print_headline")
@ColumnInfo(name = "headline_print")
private String headline_print;
@NonNull
public int getUid() {
return uid;
}
public void setUid(@NonNull int uid) {
this.uid = uid;
}
@NonNull
public String getArticle_original_id() {
return article_original_id;
}
public void setArticle_original_id(@NonNull String article_original_id) {
this.article_original_id = article_original_id;
}
public String getHeadline_main() {
return headline_main;
}
public void setHeadline_main(String headline_main) {
this.headline_main = headline_main;
}
public String getHeadline_content_kicker() {
return headline_content_kicker;
}
public void setHeadline_content_kicker(String headline_content_kicker) {
this.headline_content_kicker = headline_content_kicker;
}
public String getHeadline_print() {
return headline_print;
}
public void setHeadline_print(String headline_print) {
this.headline_print = headline_print;
}
}
多媒体:
@Entity(tableName = "multimedia",
foreignKeys = @ForeignKey(entity = Article.class,
parentColumns = "original_id",
childColumns = "article_original_id",
onDelete = CASCADE),
indices = {@Index("article_original_id")})
public class Multimedia {
@Expose(deserialize = false)
@NonNull
@PrimaryKey(autoGenerate = true)
private int uid;
@Expose(deserialize = false)
@NonNull
@ColumnInfo(name = "article_original_id")
private String article_original_id;
// Json - API
@SerializedName("type")
@ColumnInfo(name = "type")
private String type;
@SerializedName("subType")
@ColumnInfo(name = "subtype")
private String subtype;
@SerializedName("url")
@ColumnInfo(name = "url")
private String url;
@SerializedName("height")
@ColumnInfo(name = "height")
private String height;
@SerializedName("width")
@ColumnInfo(name = "width")
private String width;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
@NonNull
public String getArticle_original_id() {
return article_original_id;
}
public void setArticle_original_id(@NonNull String article_original_id)
{
this.article_original_id = article_original_id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSubtype() {
return subtype;
}
public void setSubtype(String subtype) {
this.subtype = subtype;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
}
答案 0 :(得分:4)
您文章的original_id应该具有唯一的约束,或者应该是主键。
@Entity(tableName = "article",
indices={
@Index(value="uid"),
@Index(value="original_id", unique = true)
})
OR
@SerializedName("_id")
@NonNull
@PrimaryKey
@ColumnInfo(name = "original_id")
private String original_id;