我想使用 firebase 来刷新我的本地数据库。
我有 2 个表,一个是具有 ID、名称...的植物,另一个是具有一对一关系的植物 ID 的价格。
我认为,我可以将 firebase 数据库数据存储在我的 Room db 中,但是本地数据库不使用生成的 id,因此 id 与 firebase id 相同。
因此,当firebase实时数据库中的植物数据发生变化时,我从本地数据库中的植物表中删除所有记录,然后插入新的植物记录,但我想保留第二个(价格)表记录,其中包含植物 ID。其实我的问题是:房间抛出异常,因为它不能删除植物表。
我读了很多,我找到了一个提示,我可以设置 onDelete = NO_ACTION 但它没有帮助。
有人可以帮忙,我该如何解决这个问题?
如果我不向 Price 表添加数据,我可以刷新 Plant 表
My PlantEntity
@Entity(tableName = "plants")
public class PlantEntity {
@PrimaryKey
private Long id;
@ColumnInfo(name = "name")
private String name;
@ColumnInfo(name = "active")
private boolean active;
My PriceEntity
@Entity(tableName = "prices",
foreignKeys = {
@ForeignKey(
entity = PlantEntity.class,
parentColumns = "id",
childColumns = "plant_id",
onDelete = NO_ACTION),
@ForeignKey(
entity = AmountUnitEntity.class,
parentColumns = "id",
childColumns = "amount_unit_id",
onDelete = NO_ACTION)})
public class PriceEntity {
@PrimaryKey(autoGenerate = true)
private Long id;
@ColumnInfo(name = "description")
private String description;
@ColumnInfo(name = "min_price")
private int minPrice;
@ColumnInfo(name = "max_price")
private int maxPrice;
@ColumnInfo(name = "plant_id")
private Long plantId;
@ColumnInfo(name = "amount_unit_id")
private Long amountUnitId;
And my PlantDao
@Insert
void insert(List<PlantEntity> plantEntityList);
@Update
void update(PlantEntity plantEntity);
@Delete
void delete(PlantEntity plantEntity);
@Query("DELETE FROM plants")
void deleteAll();
如果您想为已删除的工厂保留 PriceEnity 行,那么您不能为工厂定义 ForeignKey,因为它是冲突的 Foreignkey 的约束。
如果您不想保留 PriceEntity 然后使用onDelete = CASCADE
,这将删除父级的子级。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句