Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room Database compile error: Field has non-unique column name

I'm getting this error on some of the fields of my class

error: Field has non-unique column name

@Entity(tableName = "Team", foreignKeys = {
    @ForeignKey(entity = Group.class, parentColumns = "id", childColumns = "groupId")},
    indices = {@Index("groupId")})
public class Team {

    @PrimaryKey
    private long id;
    private long groupId;
    @SerializedName("Team")
    private String name;
    private String englishName;
    @SerializedName("Played")
    private int played;
    @SerializedName("Victories")
    private int win;
    @SerializedName("Draws")
    private int draw;
    @SerializedName("Defeats")
    private int defeat;
    @SerializedName("Made")
    private int goalFor;
    @SerializedName("Let")
    private int goalAgainst;
    @SerializedName("Diff")
    private int goalDiff;
    @SerializedName("Points")
    private int points;

    public Team() {

    }

    /* getter and setter methods */
}

For example, i get this error on "win" , "draw", "groupId". But not on "id" or "name". And as you can see it's a compile error and it doesn't provide anymore information about the error except that sentence in the title.

Edit: I tried changing the name of variables but it didn't work.

Edit: Getter and setter methods for "win", other methods look exactly like this one.

public int getWin() {
    return win;
}

public void setWin(int win) {
    this.win = win;
}
like image 242
Peyman Avatar asked Apr 27 '18 11:04

Peyman


2 Answers

You need to add a prefix in order to avoid columns name duplication. From the official documentation:

prefix String prefix () Specifies a prefix to prepend the column names of the fields in the embedded fields.

For the example above, if we've written:

@Embedded(prefix = "foo_") Coordinates coordinates;

https://developer.android.com/reference/android/arch/persistence/room/Embedded.html#prefix()

like image 67
Santiago Carrillo Avatar answered Oct 04 '22 20:10

Santiago Carrillo


I found a solution (well, not actually a solution). I have another entity called "Group":

@Entity
public class Group {

    @PrimaryKey
    private long id;
    private String name;
    @Embedded
    private List<Team> teams;

    public Group() {

    }

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

    /* getter and setter methods */

Turns out that The variable "teams" with "Embedded" annotation was the source of my problems. when i remove it, the code works just fine. If someone could explain to me what i did wrong (or did i?) i'd appreciate it.

Edit: Found some links related to this problem.

Android Room @Embedded annotation compilation fails for @NonNull annotated constructor parameters of a POJO defined in a library module

https://github.com/googlesamples/android-architecture-components/issues/318

like image 38
Peyman Avatar answered Oct 04 '22 21:10

Peyman