I have a database that stores data in Receipt and File entities, these entities have a one-to-many relationship, one Receipt can have several Files. I need to select Receipts with Files but not all fields from these tables, for example, I want to select id
, description
, createdAt
from the Receipt entity and imageUrl
from the File entity, is this possible? These entities contain many other fields that I don’t need right now, I want to create a list that displays the basic information
I tried somethink like that, but i have the following error “Cannot figure out how to read this field from a cursor”:
@Dao
abstract class ReceiptDao {
@Query(
"""
SELECT id, description, createdAt, (SELECT imageUrl FROM files WHERE receiptId == :id) as listOfImageUrls
FROM receipts
WHERE id == ":id"
ORDER BY createdAt DESC
""" )
abstract fun findReceiptsWithFilesById(id: Long): List<ReceiptWithFiles>
}
class ReceiptWithFiles(
val id: Long,
val description: String? = null,
val createdAt: LocalDateTime? = null,
val listOfImageUrls : List<String> = listOf(),
)
I don’t have enough experience with room and sql and I’m not sure if this is possible