有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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;

我好像找不出是怎么回事


共 (2) 个答案

  1. # 1 楼答案

    该消息是一个警告,可能还有其他错误(请参阅第2行)。e、 g

    enter image description here

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

    但是,不需要在locationId上有一个(附加)索引,因为它已经被索引为主键(这将是一种浪费,而且效率低下)。因此,建议您使用:-

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

    我认为您的总体问题是,您引用的是列的对象变量名,如果您有@ColumnInfo(name ="????"),那么您应该引用给定的名称

    • i、 e????是基础表中的列名

    您还应该在行程中使用位置\u Id而不是Id

    @Entity(tableName = "trip_table", indices = {@Index(value = {"location_Id"}, unique = true)})
    
  2. # 2 楼答案

    当我有很多外键时,我遇到了这个问题。我发现(经过多次尝试和错误后)我的索引设置错误。。。哎呀P

    错误:

    indices = [
        Index("active_color_style_id",
            "ambient_color_style_id",
            "hour_hand_dimensions_id",
            "minute_hand_dimensions_id",
            "second_hand_dimensions_id")
    ]
    

    右侧:

    indices = [
        Index("active_color_style_id"),
        Index("ambient_color_style_id"),
        Index("hour_hand_dimensions_id"),
        Index("minute_hand_dimensions_id"),
        Index("second_hand_dimensions_id")
    ]
    

    完整示例:

    @Entity(
        tableName = "analog_watch_face_table",
        foreignKeys = [
            ForeignKey(
                entity = WatchFaceColorStyleEntity::class,
                parentColumns = ["id"],
                childColumns = ["active_color_style_id"],
                onDelete = NO_ACTION
            ),
            ForeignKey(
                entity = WatchFaceColorStyleEntity::class,
                parentColumns = ["id"],
                childColumns = ["ambient_color_style_id"],
                onDelete = NO_ACTION
            ),
            ForeignKey(
                entity = WatchFaceArmDimensionsEntity::class,
                parentColumns = ["id"],
                childColumns = ["hour_hand_dimensions_id"],
                onDelete = NO_ACTION
            ),
            ForeignKey(
                entity = WatchFaceArmDimensionsEntity::class,
                parentColumns = ["id"],
                childColumns = ["minute_hand_dimensions_id"],
                onDelete = NO_ACTION
            ),
            ForeignKey(
                entity = WatchFaceArmDimensionsEntity::class,
                parentColumns = ["id"],
                childColumns = ["second_hand_dimensions_id"],
                onDelete = NO_ACTION
            )
        ],
        indices = [
            Index("active_color_style_id"),
            Index("ambient_color_style_id"),
            Index("hour_hand_dimensions_id"),
            Index("minute_hand_dimensions_id"),
            Index("second_hand_dimensions_id")
        ]
    )