我正在使用Room技术来开发我的Android应用。其中一个表格(stock_move)包含多个外键。在某些情况下,我需要插入一条没有外键(locationDestId)的stock_move。但是,在这种情况下,SQLite会抛出一个错误:
io.reactivex.exceptions.OnErrorNotImplementedException: foreign key constraint failed (code 19)
以下是我的实体:
@Entity(tableName = "stock_move",
foreignKeys = {
@ForeignKey(
entity = LocationEntity.class,
parentColumns = "id",
childColumns = "location_id",
onDelete = CASCADE
),
@ForeignKey(
entity = LocationEntity.class,
parentColumns = "id",
childColumns = "location_dest_id",
onDelete = CASCADE
),
@ForeignKey(
entity = ProductEntity.class,
parentColumns = "id",
childColumns = "product_id",
onDelete = CASCADE
),
@ForeignKey(
entity = UomEntity.class,
parentColumns = "id",
childColumns = "uom_id",
onDelete = CASCADE
)
}
)
public class StockMoveEntity {
@PrimaryKey(autoGenerate = true)
private int id;
private String name;
@ColumnInfo(name="product_id")
private int mProductId;
@ColumnInfo(name="uom_id")
private int mUomId;
@ColumnInfo(name="location_id")
private int mLocationId;
@ColumnInfo(name="location_dest_id")
private int mLocationDestId;
private int mProductQty;
public StockMoveEntity(int id, String name, int productId, int uomId, int locationId, int locationDestId, int productQty) {
this.id = id;
this.name = name;
mProductId = productId;
mUomId = uomId;
mLocationId = locationId;
mLocationDestId = locationDestId;
mProductQty = productQty;
}
}