如何在会议室数据库中添加主键?

Nim*_*ili 0 android android-sqlite android-database android-room

我有一个sqlite数据库,我想将数据库更改为Room database

表之一没有任何主键,只有两个外键。

我使用以下查询在房间之前创建了表:

CREATE TABLE student_performance(
class_id int , 
student_id char(10), 
class_date date , 
absent boolean DEFAULT 0, 
delay boolean DEFAULT 0, 
positive int DEFAULT 0, 
negative int DEFAULT 0, 
quiz float ,
FOREIGN KEY (student_id , class_id) REFERENCES student(student_id , class_id) 
ON DELETE CASCADE 
ON UPDATE CASCADE);
Run Code Online (Sandbox Code Playgroud)

现在我为房间定义表:

@Entity(tableName = "performance",
    foreignKeys = {@ForeignKey(
            entity = StudentEntry.class,
            parentColumns = {CLASS_ID, STUDENT_ID},
            childColumns = {CLASS_ID, STUDENT_ID},
            onDelete = CASCADE, onUpdate = CASCADE)})
public class PerformanceEntry {
    .
    .
    .
}
Run Code Online (Sandbox Code Playgroud)

但是它给出了错误:

error: An entity must have at least 1 field annotated with @PrimaryKey
Run Code Online (Sandbox Code Playgroud)

我不知道如何为房间数据库定义此表。

Mar*_*ler 5

存在和注释CREATE TABLE时,不必运行SQL 。添加一个主键(因为几乎没有唯一性):tableName@ColumnInfoentry_idclass_id

@Entity(
    tableName = "performance",
    foreignKeys = {
        @ForeignKey(
            entity = StudentEntry.class,
            parentColumns = {CLASS_ID, STUDENT_ID},
            childColumns = {CLASS_ID, STUDENT_ID},
            onDelete = CASCADE,
            onUpdate = CASCADE
       )
   }
)
public class PerformanceEntry  {

    /* Fields */
    @ColumnInfo(name = "entry_id")
    @PrimaryKey(autoGenerate = true)
    private int entryId;

    ...
}
Run Code Online (Sandbox Code Playgroud)