我正在使用Room来设置一个包含3个表的数据库。我想要从“文章”表中为2个表(Headline和Multimedia)设置外键,但不是主键。
请参阅以下代码。我认为我使用了正确的注释,但仍然出现以下错误:
error: com.articlesregistry.data.Multimedia具有一个外键(article_original_id),它引用com.articlesregistry.data.Article(original_id),但com.articlesregistry.data.Article没有对这些列或其主键进行唯一索引。SQLite要求在引用父列时具有唯一约束条件,因此您必须向com.articlesregistry.data.Article添加具有(original_id)列的唯一索引。
以下是相关类:
Article:
请参阅以下代码。我认为我使用了正确的注释,但仍然出现以下错误:
error: com.articlesregistry.data.Multimedia具有一个外键(article_original_id),它引用com.articlesregistry.data.Article(original_id),但com.articlesregistry.data.Article没有对这些列或其主键进行唯一索引。SQLite要求在引用父列时具有唯一约束条件,因此您必须向com.articlesregistry.data.Article添加具有(original_id)列的唯一索引。
以下是相关类:
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;
}
}