Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite requires having a unique constraint (Android Room annotations)

I'm using Room to setup a database with 3 tables. I'm trying to set foreign keys for 2 tables(Headline & Multimedia) from the "Articles" table, but not the primary key.

Please see the code below. I think I used the annotations correct, but it still gives me the following error:

error: com.articlesregistry.data.Multimedia has a foreign key (article_original_id) that references com.articlesregistry.data.Article (original_id) but com.articlesregistry.data.Article does not have a unique index on those columns nor the columns are its primary key. SQLite requires having a unique constraint on referenced parent columns so you must add a unique index to com.articlesregistry.data.Article that has (original_id) column(s).

And the classes:

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

Headline:


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

Multimedia:


 @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;
    }
}
like image 786
jorjSB Avatar asked Jul 14 '18 15:07

jorjSB


1 Answers

Your Article's original_id should have unique constraints OR should be the primary key.

@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;
like image 158
Man Avatar answered Nov 20 '22 17:11

Man