agrosner / agrosner/DBFlow

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

Aperta
#1,725 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Kotlin
Stelle
4.8k
Fork
593
Merge medio
1m
PR unite (30g)
1

Descrizione

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`
```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.