[我正在尝试使用Android房间库提供的外键注释将“交易”表链接到“信封”表。基本上,我希望信封内有多个交易。
我已经尝试将@Column_info(names)
添加到列中,但是它没有做任何事情。
[我还尝试检查传递给Transaction的构造函数的值是否确实引用了信封表上的rowId,并且确实如此。
这些是我的表POJO:
@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;
@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),?,?,?,?,?)"
我认为问题可能是您将您的“工作实体”注释为FTS索引的实体,这使得主键不再可用作外键(此处的文档有点不清楚)。
您可以将@Fts3
放到信封类上,并创建一个额外的FTS实体,该实体使用@Fts4
映射到原始实体(可让您指定内容实体)。
@Fts4(contentEntity = Envelope.class)
@Entity(tableName = "envelopes_fts")
public class EnvelopeFts {
// The fields you want to index with getters and setters
}
至少在将源实体和相应的FTS实体拆分为两个单独的Room-entity之后,至少我只能让外键再次工作。