Android DBflow使2个表之间的关系变得简单

Android DBflow使2个表之间的关系变得简单,android,dbflow,Android,Dbflow,DBFlow for android没有很好的文档,在阅读了关于如何在两个表之间建立简单关系的更多信息后,我无法做到这一点 我有两张表: 主要类别和子类别 在SubCategories中,我有channelId,它存储在maincegories中,maincegories有更多关于SubCategories表的数据,(一对多) 现在我正试图实现这种关系: 我的代码不正确,与库文档类似 主要类别: @Table(database = AppDatabase.class) public class

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

我有两张表:

主要类别
子类别

SubCategories
中,我有
channelId
,它存储在
maincegories
中,
maincegories
有更多关于
SubCategories
表的数据,(一对多)

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

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

主要类别

@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() {
        ...
    }

    ...
}
@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;

    ...
}
如何在
ChannelContentCategories
表上的
channelId
channelsubcentcategories
表上的
parentId
之间建立简单关系


提前感谢

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

从表\u name1、表\u name2、表\u name3中选择列\u name.Table\u name1、列\u name.Table\u name2、列\u name.Table\u name3,其中id.Table\u name1==id.Table\u name2&&id.Table\u name1==id.Table\u name3


希望它能有所帮助

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

从表\u name1、表\u name2、表\u name3中选择列\u name.Table\u name1、列\u name.Table\u name2、列\u name.Table\u name3,其中id.Table\u name1==id.Table\u name2&&id.Table\u name1==id.Table\u name3


希望对您有所帮助

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

像这样

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

是否要将channelId用作MainCategories的外键

像这样

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

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

  • ChannelContentCategories
    中的
    @PrimaryKey
    id
    移动到
    channelId
  • parentId
    引用到
    id
    而不是
    channelId
    ,因此无需将
    channelId
    作为
    PrimaryKey
  • 或者在表中有多个主键。但是在这种情况下,不能将
    id
    主键定义为
    自动递增的
所以,我以第三种方式结束,看看我们有什么:

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;
    }
    ...
} 
@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;
    ...
} 

我已经测试过了,效果很好。请随意选择最适合您的方式。

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

  • ChannelContentCategories
    中的
    @PrimaryKey
    id
    移动到
    channelId
  • parentId
    引用到
    id
    而不是
    channelId
    ,因此无需将
    channelId
    作为
    PrimaryKey
  • 或者在表中有多个主键。但是在这种情况下,不能将
    id
    主键定义为
    自动递增的
所以,我以第三种方式结束,看看我们有什么:

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;
    }
    ...
} 
@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;
    ...
} 

我已经测试过了,效果很好。请随意选择最适合您的方式。

您检查过这个问题了吗,您应该使用@ForeignKey还@Ammar这个库的文档不是很好,我不明白我该如何建立这种关系。我认为该文档非常陈旧,请参见此链接中这部分文档的示例:
One-To-Many
:您是否检查了此问题,您应该使用@ForeignKey也@Ammar此库的文档不是很好,我不明白我如何建立此关系。我觉得那份文件很旧,,请参阅此部分文档的示例:
One-To-Many
在此链接中:对于
ChannelContentCategories
表,我有更多关于
channelsubcentcategories
表的数据,该表在
channelsubcentcategories
中引用了
channelId
ChannelContentCategories
表,是的,就像您的样本一样
ChannelContentCategories
表我有更多关于
channelsubcentcategories
表的数据,
channelsubcentcategories
表中的
channelId
,是的,就像您的样本一样,我收到此错误,先生:
错误:**=============*class com.raizlabs.android.dbflow.processor.definition.column.ForeignKeyColumnDefinition(Kotlin反射不可用):无法从名为parentId的引用中找到引用的列channelId*=========*
对于这行代码:
引用=@ForeignKeyReference(columnName=“parentId”,foreignKeyColumnName=“channelId”)
我再次清理和重建,问题没有很好地解决,您使用的DBFlow版本是什么?你把我所有的代码都复制粘贴了吗?这个错误意味着,你忘了将
@PrimaryKey
添加到
channelId
列。是的,我复制粘贴,很抱歉,我从id中删除了primary,并添加了
channelId
,我使用该库的
4.0.0-beta7
版本,我有这个问题yetWell,我只是做了同样的事情(从
id
中删除了主键),它适用于我。请确保您与我的代码示例没有差异。您的代码和我更改的代码似乎都是相同的。ChannelSubcentCategories:
http://paste.debian.net/971902/
和ChannelContentCategories