我是 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 条建议行)。例如
使用 indices = {@Index(value = {"locationId"}, unique = true),@Index(value = {"trip_Id"})})
应该克服这个警告。
然而 ,没有必要在 上有一个(额外的)索引locationId 因为它已经被索引为主键(这将是一种浪费而且效率低下)。所以建议你使用:-
indices = {@Index(value = {"trip_Id"})})
我相信你的整体问题是你指的是列的对象变量名,如果你有
@ColumnInfo(name ="????")
那么你应该指的是给定的名字您还应该使用 location_Id 而不是 编号 在旅途中:-
@Entity(tableName = "trip_table", indices = {@Index(value = {"location_Id"}, unique = true)})
关于java - 房间编译问题 - 列引用了外键,但它不是索引的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58593506/