我正在尝试学习Room数据库中的关系,但是有一些问题。我想将Expense设置为Date,但应用程序崩溃且logcat显示此错误:原因:android.database.sqlite.SQLiteConstraintException:FOREIGN KEY约束失败(代码787)。粘贴这些类时不使用设置方法和获取方法。
日期:
@Entity(tableName = "dates")
public class Date {
@PrimaryKey(autoGenerate = true)
private int id;
private Long dateLong = System.currentTimeMillis();
private String date = new SimpleDateFormat("MM/yyyy").format(new java.util.Date(dateLong));
private int month;
private int week;
private int day;
private int dayOfWeek;
private String weekDay
public Date(int month, int week, int day, int dayOfWeek, String weekDay) {
this.month = month;
this.week = week;
this.day = day;
this.dayOfWeek = dayOfWeek;
this.weekDay = weekDay;
}
费用:
@Entity(tableName = "expense_table",
foreignKeys = @ForeignKey(entity = com.example.test.Date.class,
parentColumns = "id",
childColumns = "dateId",
onDelete = CASCADE),
indices = @Index("dateId"))
public class Expense {
@PrimaryKey(autoGenerate = true)
private int id;
private int dateId;
private String note;
private Double value;
private String type;
public Expense(Double value, String note, String type) {
this.value = value;
this.note = note;
this.type = type;
}