是的,只需更改外键类型即可long
to Long
.
在爪哇,long
类型不能是null
因此 Room 生成此列为NOT NULL
。另外,C
类没有@PrimaryKey
指定的。
@Entity(tableName = "C", foreignKeys = {@ForeignKey(entity = A.class, parentColumns = "id", childColumns = "foreign_id_a"), @ForeignKey(entity = B.class, parentColumns = "id", childColumns = "foreign_id_b")})
class C{
@PrimaryKey
public long id;
public Long foreign_id_a;
public Long foreign_id_b;
}
在科特林中,Long
类型非空。如果要插入可为空的外键,则需要将字段更改为可为空的类型Long?
.
@Entity(tableName = "C", foreignKeys = [ForeignKey(entity = A::class, parentColumns = ["id"], childColumns = ["foreign_id_a"]), ForeignKey(entity = B::class, parentColumns = ["id"], childColumns = ["foreign_id_b"])])
class C{
@PrimaryKey
var id: Long = 0
var foreign_id_a: Long? = null
var foreign_id_b: Long? = null
}