3

I am confused on how to declare Foreign Key in DBFlow. In getting started page for relationship for DBFlow (https://github.com/Raizlabs/DBFlow/blob/f2d90db39a4bf5ffcc0f22032ae20d5328b8d3c3/usage2/Relationships.md), it has example of Queen class but not the Ant nor Colony class.

Does anyone have the complete example that contains Queen + Ant + Colony classes?

Thanks

Sam
  • 1,826
  • 26
  • 58

1 Answers1

3

this is sample code which you want:

parent table:

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

    @Column
    private int categoryServerId;

    @PrimaryKey
    @Column
    public int cafeServerId;

    @Column
    private String title;

    @Column
    private boolean isAuto;

    public List<CafeWebChildContentCategories> subContentCategories;

    @OneToMany(methods = {OneToMany.Method.ALL}, variableName = "subContentCategories")
    public List<CafeWebChildContentCategories> getMyChannelSubContentCategories() {
        if (subContentCategories == null || subContentCategories.isEmpty()) {
            subContentCategories = SQLite.select()
                    .from(CafeWebChildContentCategories.class)
                    .where(CafeWebChildContentCategories_Table.parentId.eq(cafeServerId))
                    .queryList();
        }
        return subContentCategories;
    }

    public CafeWebContentCategories() {
    }

    // setters and getters
}

child table:

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

    @Column
    public int categoryId;

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

    @Column
    private String title;

    public CafeWebChildContentCategories() {
    }

    // setters and getters
}

please ask me if you have question about that

DolDurma
  • 15,753
  • 51
  • 198
  • 377
  • Hi Mahdi, It looks OK, so I accepted your answer. Unfortunately, your example comes a bit late. I've decided to use realm instead. – Sam Jul 18 '17 at 12:20
  • 1
    @Sam dont use `realm` because size of realm on each release new version is bigger that with old version, i migrate all my projects from that to dbflow, dfblow is very useful than realm – DolDurma Jul 18 '17 at 17:31
  • Hi Mahdi, I tried to use DBFlow as I read that it is very fast. However, it is hard to get support on DBFlow. The documentation is also not that great. But I'll definitely look into it when DBFlow is more mature :) – Sam Jul 19 '17 at 04:10
  • @Sam sqlite and wrapper library as DBFlow is slower than realm. and i have to switch to realm in my big project – DolDurma Oct 22 '17 at 17:35