Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to load list of objects with related objects (annotated with ForeignKey)? #1725

Open
milovan92 opened this issue Jun 3, 2021 · 0 comments

Comments

@milovan92
Copy link

DBFlow Version: 4.2.4

Bug or Feature Request: /

Description:

How to load list of objects with related objects (annotated with ForeignKey)?

Code example:

@Table(database = Database::class)
class A : BaseModel() {
    @Column @PrimaryKey var id: Int = 0
    @Column var name = ""
    @Column @ForeignKey(onDelete = ForeignKeyAction.RESTRICT, saveForeignKeyModel = true)
    var b: B? = null
}
@Table(database = Database::class)
class B : BaseModel() {
    @Column @PrimaryKey var id: Int = 0
    @Column var name = ""
}

I need to load list of A objects populated with B object. When I load 100 objects of class A and then iterate on them one by one and access B through them, DBFlow fires 100 SELECT queries. I wish to somehow load B's together with A's in a single query, and then populate A's with B's so no further queries are fired. So I was expecting that if query has INNER JOIN that it will automatically populate A object with B objects. Executed query:

SELECT DISTINCT `a`.`id`,`a`.`name`,`b`.`id`,`b`.`name` FROM `A` AS `a` INNER JOIN `B` AS `b` ON `a`.`b_id`=`b`.`id`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant