Room Database compile error: Field has non-unique column name
Room Database compile error: Field has non-unique column name
我在 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 */
}
例如,我在 "win"、"draw"、"groupId" 上收到此错误。但不是 "id" 或 "name"。如您所见,这是一个编译错误,除了标题中的那句话外,它不再提供有关错误的更多信息。
编辑:我尝试更改变量的名称,但没有成功。
编辑: Getter 和 setter 方法用于 "win",其他方法与此完全相同。
public int getWin() {
return win;
}
public void setWin(int win) {
this.win = win;
}
我找到了解决方案(好吧,实际上不是解决方案)。
我有另一个名为 "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 */
事实证明,带有 "Embedded" 注释的变量 "teams" 是我问题的根源。当我删除它时,代码工作正常。如果有人可以向我解释我做错了什么(或者我做错了什么?),我将不胜感激。
编辑:找到一些与此问题相关的链接。
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
您需要添加前缀以避免列名重复。来自官方文档:
前缀
字符串前缀 ()
指定前缀以在嵌入字段中添加字段的列名。
对于上面的例子,如果我们写成:
@Embedded(prefix = "foo_") Coordinates coordinates;
https://developer.android.com/reference/android/arch/persistence/room/Embedded.html#prefix()
我在 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 */
}
例如,我在 "win"、"draw"、"groupId" 上收到此错误。但不是 "id" 或 "name"。如您所见,这是一个编译错误,除了标题中的那句话外,它不再提供有关错误的更多信息。
编辑:我尝试更改变量的名称,但没有成功。
编辑: Getter 和 setter 方法用于 "win",其他方法与此完全相同。
public int getWin() {
return win;
}
public void setWin(int win) {
this.win = win;
}
我找到了解决方案(好吧,实际上不是解决方案)。 我有另一个名为 "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 */
事实证明,带有 "Embedded" 注释的变量 "teams" 是我问题的根源。当我删除它时,代码工作正常。如果有人可以向我解释我做错了什么(或者我做错了什么?),我将不胜感激。
编辑:找到一些与此问题相关的链接。
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
您需要添加前缀以避免列名重复。来自官方文档:
前缀 字符串前缀 () 指定前缀以在嵌入字段中添加字段的列名。
对于上面的例子,如果我们写成:
@Embedded(prefix = "foo_") Coordinates coordinates;
https://developer.android.com/reference/android/arch/persistence/room/Embedded.html#prefix()