Update failing on Self Referential Foreign key with Room update method

会有一股神秘感。 提交于 2019-12-11 15:59:12

问题


I need to create folder structure using Room. I had followed this approach.

@Entity(foreignKeys = @ForeignKey(entity = NodeEntity.class,
        parentColumns = "id",
        childColumns = "parentId", onDelete = CASCADE), indices = {@Index(value = {"parentId"})})
public class NodeEntity {
    @PrimaryKey
    @NonNull
    private String id;
    private int isLeafItem;
    private int level;
    private String name;
    private String parentId;
    private double price;
    private int quantity;
    private String tags;
    @TypeConverters(DateConverter.class)
    private Date createdAt;
}

My Node DAO is like this.

@Dao
public interface NodeDao {

    @Insert(onConflict = REPLACE)
    void createNode(NodeEntity nodeEntity);

    @Query("SELECT * FROM NodeEntity WHERE parentId = :parentId ")
    Flowable<List<NodeEntity>> getNodesByParentId(String parentId);

    @Query("SELECT * FROM NodeEntity WHERE level = :level")
    Flowable<List<NodeEntity>> getNodesByLevel(int level);

    @Query("SELECT * FROM NodeEntity WHERE id = :nodeId")
    Single<NodeEntity> getNode(String nodeId);

    @Update(onConflict = REPLACE)
    void updateNode(NodeEntity node);

    @Delete
    void deleteNode(NodeEntity nodeEntity);
}

All operations are working fine. but if I tried to move one folder to another folder, that means you need to change the parentId of the folder you are moving. When I do that, the update is failing without any exception.

so I ended up writing a separate method like this.

@Query("UPDATE NodeEntity SET parentId = :newParentId, level=:newLevelId where id=:id AND parentId=:parentId")
    void moveNode(String newParentId,int newLevelId, String id, String parentId);

Above move, method is working as expected. But not sure, why @update is silently failing.

来源:https://stackoverflow.com/questions/46691663/update-failing-on-self-referential-foreign-key-with-room-update-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!