创建具有两个可能父项的房间实体的正确方法是什么?

What's the correct way to create a Room Entity with two possible Parents?

我是第一次在 Android 中使用 Room。我有一个实体 Comment,它是树结构的一部分。一些评论将是 Post 的子评论,而其他评论将是另一个 Comment.

的子评论

以下是我的实体声明

@Entity(tableName = "comments,
    foreignKeys = {
        @ForeignKey(entity = Post.class, parentColumns = "uid",
                childColumns = "post_uid", onDelete = CASCADE),
        @ForeignKey(entity = Comment.class, parentColumns = "uid",
                childColumns = "comment_uid", onDelete = CASCADE)
    }
)
public class Comment {
   ...
}

我看不到 Foreign Key 的 nullable 或 required 之类的选项,那么声明键可以丢失的正确方法是什么?

只要底层列允许 NULL,就应该没问题。 This sample app 展示了一个树结构,尽管它只有一个可能的父节点。不过,树的根有一个 null 父 ID,如果可行,其他外键也应该 null