Skip to content Skip to sidebar Skip to footer

Android Create Simple OneToMany Relation With DBFlow

i have simple two models and i'm trying to create simple OneToMany between them, as far as i'm newbie to use this library i can't use library documentation, my main model is: @Tabl

Solution 1:

Don't know which version of DBFlow you are using but adding @ModelContainer annotation to ModelChannelPosts might help you


Solution 2:

Can you please check again by remove '@Column' from following code segment:

@Column
private List<ModelVideos> channel_video_containers;

Note: You might see an error of no setter for channel_video_containers so create one.

And put a foreign key in ModelVideos since many ModelVideos can belong to single ModelChannelPosts. e.g

In ModelVideos class

@Column
@ForeignKey(saveForeignKeyModel = false)
ModelChannelPosts modelChannelPosts;

Rest looks fine. I am keeping in my mind the version of DBFlow = 4.0.0-beta3 (which is latest as per now)


Solution 3:

This might be late but it would be helpfull for other developers. One to Many Relation will be like this

@Table(database = AppDatabase.class)
public class ModelChannelPosts extends BaseModel {

    @PrimaryKey
    private int id;

    @Column
    private String channelId;

    private List<ModelVideos> channel_video_containers;

    @Column
    private String createdAt;

    @Column
    private String updatedAt;


    public ModelChannelPosts() {
    }

    @OneToMany(methods = {OneToMany.Method.ALL}, variableName = "channel_video_containers")
    public List<ModelVideos> getVideos() {
        if (channel_video_containers == null || channel_video_containers.isEmpty()) {
            channel_video_containers = new Select()
                    .from(ModelVideos.class)
                    .where(ModelVideos_Table.modelChannelPosts_id.eq(id))
                    .queryList();
        }
        return channel_video_containers;
    }

    ...
}

and ModelVideos class will be like

@Table(database = AppDatabase.class)
public class ModelVideos extends BaseModel {

    @PrimaryKey
    private int id;

    @Column
    private String fileName;

    @Column
    private String videoLength;

    @Column
    private int channelId;

    @ForeignKey(stubbedRelationship = true)
    ModelChannelPosts modelChannelPosts;

    public ModelVideos() {
    }

    ...
}

Post a Comment for "Android Create Simple OneToMany Relation With DBFlow"