DBflow在2个表之间建立简单的关系

时间:2017-06-04 15:20:29

标签: android dbflow

Android的DBFlow没有好文档,在阅读了有关如何在两个表之间建立简单关系的更多信息后,我无法做到这一点。

我有两张桌子:

MainCategoriesSubCategories

SubCategorieschannelId存储MainCategoriesMainCategories SubCategories表上有更多数据,(一对多)

现在我正试图实现这种关系:

我的代码不正确,与库文档

相似

MainCategories

@Table(database = AppDatabase.class)
public class ChannelContentCategories extends BaseModel {
    @PrimaryKey(autoincrement = true)
    private int id;

    @Column
    private int channelId;

    @Column
    private String title;

    List<ChannelSubContentCategories> subContentCategories;

    @OneToMany(methods = {OneToMany.Method.ALL})
    public List<ChannelSubContentCategories> getMyChannelSubContentCategoriess() {
        ...
    }

    ...
}

SubCategories

@Table(database = AppDatabase.class)
public class ChannelSubContentCategories extends BaseModel {
    @PrimaryKey(autoincrement = true)
    private int id;

    @Column
    private int categoryId;

    @Column
    private int parentId;

    @Column
    private String title;

    ...
}

如何在channelId表格ChannelContentCategoriesparentId表格ChannelSubContentCategories之间建立简单关系?

提前致谢

3 个答案:

答案 0 :(得分:1)

此处的问题是DBFlow仅支持ForeignKey引用另一个表的PrimaryKey列。因此,您有3种不同的方式来实现您的关系:

  • 将您的@PrimaryKey移至ChannelContentCategoriesid移至channelId
  • parentId引用id代替channelId,因此channelId不需要PrimaryKey
  • 或者表中有多个主键。但在这种情况下,无法将您的id主键定义为autoincremented

所以,我最终得到了第三种方式,看看我们拥有的东西:

<强> ChannelContentCategories.java

@Table(database = AppDatabase.class)
public class ChannelContentCategories extends BaseModel {
    @PrimaryKey
    @Column
    public int id;

    @PrimaryKey
    @Column
    public int channelId;

    @Column
    public String title;

    public List<ChannelSubContentCategories> subContentCategories;

    @OneToMany(methods = {OneToMany.Method.ALL}, variableName = "subContentCategories") // this should be equal to the field name, that contains children
    public List<ChannelSubContentCategories> getMyChannelSubContentCategories() {
        if (subContentCategories == null || subContentCategories.isEmpty()) {
            subContentCategories = SQLite.select()
                    .from(ChannelSubContentCategories.class)
                    .where(ChannelSubContentCategories_Table.parentId.eq(channelId))
                    .queryList();
        }
        return subContentCategories;
    }
    ...
} 

<强> ChannelSubContentCategories.java

@Table(database = AppDatabase.class)
public class ChannelSubContentCategories extends BaseModel {
    @PrimaryKey(autoincrement = true)
    @Column
    public int id;

    @Column
    public int categoryId;

    @Column
    @ForeignKey(tableClass = ChannelContentCategories.class,
            references = @ForeignKeyReference(columnName = "parentId", foreignKeyColumnName = "channelId"))
    public int parentId;

    @Column
    public String title;
    ...
} 

我测试了它并且它有效。随意选择最合适的方式。

答案 1 :(得分:0)

您是否希望将channelId用作MainCategories的外键?

像这样? :

@ForeignKey(tableClass = SubCategories.class)
@Column
private int channelId;

答案 2 :(得分:-1)

您不必从数据库加载数据,然后将其关联起来。只需使用SQL语句来组合所有这些信息。

SELECT column_name.Table_name1, column_name.Table_name2, column_name.Table_name3 FROM Table_name1, Table_name2, Table_name3 WHERE id.Table_name1 == id.Table_name2 && id.Table_name1 == id.Table_name3;

希望它会有所帮助