1

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

我有2张桌子:

MainCategoriesSubCategories

SubCategories我有channelId它存储在表中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;

    ...
}

我怎样才能channelIdChannelContentCategories桌子和parentId桌子之间建立简单的关系ChannelSubContentCategories

提前致谢

4

3 回答 3

1

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

  • 将您@PrimaryKeyChannelContentCategories移至idchannelId
  • 使parentId被引用id而不是channelId,因此不需要channelIdPrimaryKey
  • 或者表中有多个主键。但在这种情况下,您不能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;
    ...
} 

我已经对其进行了测试,并且可以正常工作。随意选择最适合您的方式。

于 2017-06-17T12:12:09.007 回答
0

您想使用 channelId 作为 MainCategories 的外键吗?

像这样?:

@ForeignKey(tableClass = SubCategories.class)
@Column
private int channelId;
于 2017-06-15T21:24:35.803 回答
-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;

希望它会有所帮助

于 2017-06-12T14:33:08.200 回答