0

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:

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

    @Column
    private String channelId;

    @Column
    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.channelId.eq(id))
                    .queryList();
        }
        return channel_video_containers;
    }

    ...
}

and that has many ModelVideos, then ModelVideos belongs to ModelChannelPosts:

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

    public ModelVideos() {
    }

    ...
}

in ModelChannelPosts_Table file model.channel_video_containers is unknown and i get this error:

Error:(168, 48) error: <identifier> expected

how can i implementing OneToMany for them?( make relation ship between channel_video_containers on ModelChannelPosts and channelId on ModelVideos ?

tux-world
  • 2,680
  • 6
  • 24
  • 55

3 Answers3

0

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

saike
  • 1
0

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)

Farhan
  • 13,290
  • 2
  • 33
  • 59
0

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

    ...
}
Rajan Maurya
  • 624
  • 1
  • 12
  • 24