java Room compile problem列引用外键,但它不是索引的一部分
我是安卓应用程序开发的新手,正在创建一个应用程序,其中包含存储位置的旅行
我得到了一个编译错误:“trip_Id列引用了一个外键,但它不是索引的一部分。每当修改父表时,这可能会触发全表扫描,因此强烈建议您创建一个覆盖此列的索引。”
我有两张桌子:Trip&;地点
我已经尝试在tripId和locationId各自的类中建立索引,但这并不能解决问题
行程有其id(PK)、标题、描述和优先级
一个位置有其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;
我好像找不出是怎么回事
# 1 楼答案
该消息是一个警告,可能还有其他错误(请参阅第2行)。e、 g
使用
indices = {@Index(value = {"locationId"}, unique = true),@Index(value = {"trip_Id"})})
应该可以克服这个警告但是,不需要在locationId上有一个(附加)索引,因为它已经被索引为主键(这将是一种浪费,而且效率低下)。因此,建议您使用:-
我认为您的总体问题是,您引用的是列的对象变量名,如果您有
@ColumnInfo(name ="????")
,那么您应该引用给定的名称您还应该在行程中使用位置\u Id而不是Id:
# 2 楼答案
当我有很多外键时,我遇到了这个问题。我发现(经过多次尝试和错误后)我的索引设置错误。。。哎呀P
错误:
右侧:
完整示例: