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:

@ColumnprivateList<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)publicclassModelChannelPostsextendsBaseModel {

    @PrimaryKeyprivateint id;

    @Columnprivate String channelId;

    private List<ModelVideos> channel_video_containers;

    @Columnprivate String createdAt;

    @Columnprivate String updatedAt;


    publicModelChannelPosts() {
    }

    @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 = newSelect()
                    .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)publicclassModelVideosextendsBaseModel {

    @PrimaryKeyprivateint id;

    @Columnprivate String fileName;

    @Columnprivate String videoLength;

    @Columnprivateint channelId;

    @ForeignKey(stubbedRelationship = true)
    ModelChannelPosts modelChannelPosts;

    publicModelVideos() {
    }

    ...
}

Post a Comment for "Android Create Simple Onetomany Relation With Dbflow"