java - 房间编译问题 - 列引用了外键,但它不是索引的一部分

标签 java android android-sqlite android-room

我是 android 应用程序开发的新手,正在创建一个包含存储位置的 Trips 的应用程序。

我收到一个编译错误:“trip_Id 列引用了一个外键,但它不是索引的一部分。这可能会在父表被修改时触发全表扫描,因此强烈建议您创建一个涵盖该列的索引。”

我有 2 张 table :旅行和位置。

我已经尝试在它们各自的类中索引tripId 和locationId,但它并没有解决问题。

旅行有它的 id (PK)、标题、描述和优先级。

一个 Location 有它的 locationId (PK)、tripId(FK)、locationName 和该地点的 LatLng。

@Entity(tableName = "location_table",
        foreignKeys = @ForeignKey(entity = Trip.class, parentColumns = "location_Id", childColumns = "trip_Id"),
        indices = {@Index(value = {"locationId"}, unique = true)})
public class Location {

    @PrimaryKey(autoGenerate = true)
    private int locationId;

    @ColumnInfo (name = "trip_Id")
    private int tripId;

    private String locationName;

    @TypeConverters(LatLngConverter.class)
    private LatLng latLng;


@Entity(tableName = "trip_table", indices = {@Index(value = {"id"}, unique = true)})

public class Trip {

    @PrimaryKey(autoGenerate = true) // with each new row SQLite will automatically increment this ID so it will be unique
    @ColumnInfo (name = "location_Id")
    private int id;

    private String title;

    private String description;

    private int priority;


我似乎无法找出问题所在

最佳答案

该消息是警告,还会有其他错误(请参阅第 2 条建议行)。例如

enter image description here

使用 indices = {@Index(value = {"locationId"}, unique = true),@Index(value = {"trip_Id"})})应该克服这个警告。

然而 ,没有必要在 上有一个(额外的)索引locationId 因为它已经被索引为主键(这将是一种浪费而且效率低下)。所以建议你使用:-

indices = {@Index(value = {"trip_Id"})})

我相信你的整体问题是你指的是列的对象变量名,如果你有 @ColumnInfo(name ="????")那么你应该指的是给定的名字
  • IE。 ????是基础表中的列名。

  • 您还应该使用 location_Id 而不是 编号 在旅途中:-
    @Entity(tableName = "trip_table", indices = {@Index(value = {"location_Id"}, unique = true)})
    

    关于java - 房间编译问题 - 列引用了外键,但它不是索引的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58593506/

    相关文章:

    java - Eclipse 崩溃,现在未生成 R 文件

    Java将一个ArrayList中的项克隆到另一个ArrayList中?

    Android compose Icon重组问题

    java - 将图像转换为二维数组仅获取左上象限

    java - 得到一个虚拟的 slf4j 记录器?

    java - 通过传递导致应用程序失败的值来一个接一个地调用异步任务以进行 Web 服务调用

    android - 需要 Android 客户端/服务器数据库教程

    java - 需要在 Jersey REST 服务中使用 SQLiteOpenHelper 但我无法传递所需的 Android 上下文

    android - Caused by : android. database.sqlite.SQLiteException: 无法将只读数据库从版本 0 升级到 1

    java - 在不进行过多 JNI 调用的情况下使用 LWJGL