Yes, just change foreign key types from long
to Long
.
In Java, long
type cannot be null
and therefore Room generates this column as NOT NULL
. Also, the C
class does not have @PrimaryKey
specified.
@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;
}
In Kotlin, Long
type is non-null. If you want to insert nullable foreign keys, you need to change the fields to nullable type 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
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…