【问题标题】:FOREIGN KEY constraint failed (code 787) in Room databaseRoom 数据库中的 FOREIGN KEY 约束失败(代码 787)
【发布时间】:2019-03-22 11:58:49
【问题描述】:

我正在尝试学习 Room 数据库中的关系,但我遇到了一些问题。我想把费用放在日期,但应用程序崩溃和 logcat 显示: 引起:android.database.sqlite.SQLiteConstraintException:外键约束失败(代码 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;
}

【问题讨论】:

    标签: android android-room


    【解决方案1】:

    您需要创建第一个日期表条目,而不是向数据库添加费用条目。欲了解更多信息,请访问this链接

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多