我正在使用 Android Room Persistence Library 作为 ORM。
我有以下实体:
@Entity(tableName = "log_entries",
foreignKeys = {
@ForeignKey(
entity = Serving.class,
parentColumns = "id",
childColumns = "foodId",
onDelete = ForeignKey.CASCADE
)
}
)
public class LogEntry {
@PrimaryKey(autoGenerate = true)
private long id;
private long servingId;
// ...
}
有些日志条目有服务,有些则没有。添加一个有服务的日志条目可以正常工作,但是添加一个 id = 0 来表示“无关系”会导致
SQLiteConstraintException
消息出现
FOREIGN KEY 约束失败
// ...
LogEntry logEntry = new LogEntry();
logEntry.setServingId(0);
// ...
db.logEntryDao().add(logEntry);
那么,如何表达日志条目在 Room 中没有服务的事实?