【问题标题】:How to create a table with a two or more foreign keys using Android Room?如何使用 Android Room 创建具有两个或多个外键的表?
【发布时间】:2017-08-31 19:04:08
【问题描述】:

根据entity-relationship modeltbl_posttbl_category之间的关系可以使用Room Persistency Library指定如下:

@Entity(foreignKeys = @ForeignKey(
    entity = TblPost.class,
    parentColumns = "id",
    childColumns = "tbl_post_id")
)
class TblPostCategory {
    @PrimaryKey
    public String id;

    @ColumnInfo(name = "user_id")
    public String postId;
}

但是 TblPostCategory 依赖于两个外键:post_idcategory_id 来自 TblPostTbCategory

应该如何使用 Room 注释来描述关系?

【问题讨论】:

    标签: android sqlite android-sqlite android-room


    【解决方案1】:

    TblCategory.java

    @Entity
    class TblCategory {
        @PrimaryKey
        @ColumnInfo(name="cat_id")
        public String id;
    
        @ColumnInfo(name = "cat_name")
        public String name;
    }
    

    TblPost.java(它缺少外键引用,但对案例并不重要)

    @Entity
    class TblPost {
        @PrimaryKey
        @ColumnInfo(name="post_id")
        public String id;
    
        public String title, content, create_time, author_id;
    }
    

    TblPostCategory.java

    @Entity(foreignKeys = {
        @ForeignKey(
            entity = TblPost.class,
            parentColumns = "post_id",
            childColumns = "tbl_post_id"
        ),
        @ForeignKey(
            entity = TblCategory.class,
            parentColumns = "cat_id",
            childColumns = "tbl_category_id"
        )
    })
    class TblPostCategory {
        @PrimaryKey
        @ColumnInfo(name="tbl_post_id")
        public String id;
    
        @ColumnInfo(name = "tbl_category_id")
        public String categoryId;
    }
    

    【讨论】:

    • 如果我只有tbl_categorytbl_post 没有tbl_post_category 的表怎么办。每个类别都有一个帖子列表。我向tbl_post 添加了一个外键,指的是category_id。但我仍然无法检索List<Post>。我不断收到此错误:android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787)
    • @yalematta 您正在尝试做的事情是“违反”数据库建模规则。这是many to many关系(n:m)的情况。阅读:tomjewett.com/dbdesign/dbdesign.php?page=manymany.php。不过,如果您希望将List<Post> 添加到tbl_category,则无需在tbl_post 中定义外键。您只需为Post 对象定义Converters,因为Room 只接受原始类型。看到这个:developer.android.com/training/data-storage/room/…
    • 如何为这个有两个外键的表创建一个返回类型?提取是使用 JOIN 语句完成的(也许?)。
    • @NaveenNiraula 返回类型是什么意思?从 TblPostCategory 表中,您只能从其他两个表中检索 id。要检索其他字段,您需要使用 JOINS
    • @joao86 是的,我的意思是问我该如何实现 join 呢?
    【解决方案2】:

    在 Kotlin 中:

    @Entity(
        tableName = "some_table",
        indices = [Index("id"), Index("brand_id"), Index("model_id")],
        foreignKeys = [
            ForeignKey(entity = BrandEntity::class, parentColumns = ["id"],
                childColumns = ["brand_id"]),
            ForeignKey(entity = ModelEntity::class, parentColumns = ["id"],
                childColumns = ["model_id"]),
            ForeignKey(entity = Table1Entity::class, parentColumns = ["id"],
                childColumns = ["table1_id"]),
            ForeignKey(entity = Table2Entity::class, parentColumns = ["id"],
                childColumns = ["table2_id"])
        ]
    )
    

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 2020-08-04
      • 1970-01-01
      相关资源
      最近更新 更多