【问题标题】:Room Database compile error: Field has non-unique column name房间数据库编译错误:字段具有非唯一的列名
【发布时间】:2018-04-27 11:42:07
【问题描述】:

我在班级的某些领域遇到了这个错误

错误:字段的列名不唯一

@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”上。如您所见,这是一个编译错误,除了标题中的那句话之外,它不再提供有关该错误的信息。

编辑:我尝试更改变量的名称,但没有成功。

编辑: "win" 的 getter 和 setter 方法,其他方法看起来和这个完全一样。

public int getWin() {
    return win;
}

public void setWin(int win) {
    this.win = win;
}

【问题讨论】:

  • 你能为win添加你的getter和setter吗?
  • 刚刚添加了方法。
  • 尝试在字段中添加@ColumnInfo(name = “xxxx”) 以指定您希望它们在SQL 表中如何被调用,也许Serialized Name 冲突?我不知道。无论如何,除非您真的需要它们,否则我会将它们公开并删除 getter/setter。
  • 试过了,没用
  • 我发现了一些东西,并用答案解释了它。

标签: java android database android-room


【解决方案1】:

您需要添加前缀以避免列名重复。来自官方文档:

前缀 字符串前缀 () 指定前缀以在嵌入字段中的字段的列名之前添加。

对于上面的例子,如果我们写了:

@Embedded(prefix = "foo_") 坐标坐标;

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

【讨论】:

    【解决方案2】:

    我找到了解决方案(嗯,实际上不是解决方案)。 我有另一个名为“组”的实体:

    @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 */
    

    事实证明,带有“嵌入式”注释的变量“团队”是我问题的根源。当我删除它时,代码工作得很好。如果有人可以向我解释我做错了什么(或者我做错了什么?),我将不胜感激。

    编辑:找到一些与此问题相关的链接。

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-13
      • 2021-10-02
      • 1970-01-01
      相关资源
      最近更新 更多