我在 Room 上遇到 "foreign key mismatch" 错误的原因是什么?
What's the reason why I'm having "foreign key mismatch" error on Room?
我正在尝试 link 我的“事务” table 到我的“信封” table 使用外键注释 Android 房间库提供。基本上,我想要一个信封里面有不止一笔交易。
我尝试过的事情:
我尝试将 @Column_info(names)
添加到列中,但它没有任何作用。
我还尝试检查我传递给 Transaction 的构造函数的值是否实际上引用了 Envelope 的 table 上的 rowId,它的确如此。
这些是我的 table POJO:
信封 class:
@Entity(tableName = "envelope")
@Fts3
public class Envelope {
@ColumnInfo(name="rowid")
@PrimaryKey(autoGenerate = true)
private int rowid;
private String name;
private float balance;
private float maxBalance;
private String description;
private Date createdAt;
private Date LastUpdatedAt;
交易class:
@Entity(foreignKeys = @ForeignKey(entity = Envelope.class,
parentColumns = "rowid",
childColumns = "envelopeId",
onDelete = ForeignKey.CASCADE))
public class Transaction {
@PrimaryKey(autoGenerate = true)
private int id;
private String name;
private float amount;
@ColumnInfo(name="envelopeId")
private int envelopeId;
private String type; //can be "income" or "expense"
@ColumnInfo(defaultValue = "CURRENT_TIMESTAMP")
private Date dateTransacted;
这里是错误:
SQLiteLog: (1) foreign key mismatch - "Transaction" referencing "envelope" in "INSERT OR ABORT INTO `Transaction` (`id`,`name`,`amount`,`envelopeId`,`type`,`dateTransacted`) VALUES (nullif(?, 0),?,?,?,?,?)"
我认为问题可能是您将 "working entity" 注释为 FTS 索引实体,这使得主键不再可用作外键(此处的文档有点不清楚)。
您可以将 @Fts3
放在您的信封 class 上并使用 @Fts4
创建一个额外的 FTS 实体映射到您的原始实体(它允许您指定内容实体) .
@Fts4(contentEntity = Envelope.class)
@Entity(tableName = "envelopes_fts")
public class EnvelopeFts {
// The fields you want to index with getters and setters
}
至少在我将源实体和相应的 FTS 实体拆分为两个单独的 Room 实体后,我才能让我的外键再次工作。
我正在尝试 link 我的“事务” table 到我的“信封” table 使用外键注释 Android 房间库提供。基本上,我想要一个信封里面有不止一笔交易。
我尝试过的事情:
我尝试将
@Column_info(names)
添加到列中,但它没有任何作用。我还尝试检查我传递给 Transaction 的构造函数的值是否实际上引用了 Envelope 的 table 上的 rowId,它的确如此。
这些是我的 table POJO:
信封 class:
@Entity(tableName = "envelope")
@Fts3
public class Envelope {
@ColumnInfo(name="rowid")
@PrimaryKey(autoGenerate = true)
private int rowid;
private String name;
private float balance;
private float maxBalance;
private String description;
private Date createdAt;
private Date LastUpdatedAt;
交易class:
@Entity(foreignKeys = @ForeignKey(entity = Envelope.class,
parentColumns = "rowid",
childColumns = "envelopeId",
onDelete = ForeignKey.CASCADE))
public class Transaction {
@PrimaryKey(autoGenerate = true)
private int id;
private String name;
private float amount;
@ColumnInfo(name="envelopeId")
private int envelopeId;
private String type; //can be "income" or "expense"
@ColumnInfo(defaultValue = "CURRENT_TIMESTAMP")
private Date dateTransacted;
这里是错误:
SQLiteLog: (1) foreign key mismatch - "Transaction" referencing "envelope" in "INSERT OR ABORT INTO `Transaction` (`id`,`name`,`amount`,`envelopeId`,`type`,`dateTransacted`) VALUES (nullif(?, 0),?,?,?,?,?)"
我认为问题可能是您将 "working entity" 注释为 FTS 索引实体,这使得主键不再可用作外键(此处的文档有点不清楚)。
您可以将 @Fts3
放在您的信封 class 上并使用 @Fts4
创建一个额外的 FTS 实体映射到您的原始实体(它允许您指定内容实体) .
@Fts4(contentEntity = Envelope.class)
@Entity(tableName = "envelopes_fts")
public class EnvelopeFts {
// The fields you want to index with getters and setters
}
至少在我将源实体和相应的 FTS 实体拆分为两个单独的 Room 实体后,我才能让我的外键再次工作。