Anthony
Anthony

Reputation: 7828

Can an @Relation be queried in Android Room

I didn't really expect this to work, but can Android Room @Relation be filtered with a WHERE clause. For example:

SELECT * FROM meta WHERE subjectId IN (?)

@RawQuery(observedEntities = [ MetadataEntity::class, SubjectJunction::class ])
internal abstract fun internalGetImages(query: SupportSQLiteQuery): LiveData<List<MetadataXmp>>

`

data class MetadataXmp @JvmOverloads constructor(
    @Embedded
    val metadata:MetadataEntity,
    @Relation(
            parentColumn = "id",
            entityColumn = "metaId",
            projection = ["subjectId"],
            entity = SubjectJunction::class)
    var subjectIds: List<Long> = Collections.emptyList())

This throws no such column: subjectId. I suspect that @Relation is only gathered based on the core query result and the relational columns are never present for the initial query, but I wanted to see if the relation could referenced.

Upvotes: 0

Views: 1579

Answers (1)

Ioannis I
Ioannis I

Reputation: 308

Maybe a bit late but you can query the @Relation. You can find an example here (see tip 6).

Upvotes: 1

Related Questions