I'm making an Android app that uses room to create and manage a database, I need to do a Join between two tables that have columns with the same name (the primary key in both tables is called "id"). I used a POJO to hold the returned tuples but It only can Hold one id (I need both id's but only Toma.id is returned).
My Query for the join:
@Query("SELECT Toma.id, statusToma, horaToma, nombreMedicamento, tipo, titulo, color, Tratamiento.id FROM TOMA JOIN Tratamiento ON Tratamiento.id = Toma.tratamientoID JOIN Medicamento ON Tratamiento.medicamentoID = Medicamento.id WHERE Tratamiento.usuarioID = :id")
fun getTomasDia(id: Int?) : LiveData<List<JoinTomasDelDia>>
My POJO:
data class JoinTomasDelDia(
@ColumnInfo(name = "id") val id: Int?,
@ColumnInfo(name = "horaToma") val horaToma: String?,
@ColumnInfo(name = "statusToma") var statusToma: Int?,
@ColumnInfo(name = "nombreMedicamento") val medicamento: String?,
@ColumnInfo(name = "tipo") val tipo: String?,
@ColumnInfo(name = "titulo") val tituloTratamiento: String?,
@ColumnInfo(name = "color") val color: Int?,
@ColumnInfo(name = "Tratamiento.id") val idTratamiento: Int?
)
I have tried use Toma.id and Tratamiento.id in the Query and also in the @ColumnInfo name for both id, but only Toma.id is returned, Tratamiento.id is returning null.
My entities:
@Entity(foreignKeys = arrayOf(
ForeignKey(entity = Tratamiento::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("tratamientoID"),
onDelete = ForeignKey.CASCADE
)
))
class Toma(@PrimaryKey(autoGenerate = true) var id: Int,
var statusToma : Int = 0,
var horaToma: String? = null,
var tratamientoID: Int? = null
)
@Entity(foreignKeys = arrayOf(
ForeignKey(entity = Medicamento::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("medicamentoID"),
onDelete = ForeignKey.CASCADE),
ForeignKey(entity = Usuario::class,
parentColumns = arrayOf("uid"),
childColumns = arrayOf("usuarioID"),
onDelete = ForeignKey.CASCADE
)
))
class Tratamiento (@PrimaryKey(autoGenerate = true) var id: Int,
var titulo: String? = null,
var usuarioID: Int? = null,
var medicamentoID: Int? = null,
var indicaciones: String? = null,
var fechaInicio: String? = null,
var fechaFin: String? = null,
var diasTratamiento: Int = 0,
var status: Int? = null,
var recordatorio: Int? = null,
var atiempo: Int? = null,
var pospuestas: Int? = null,
var omitidas: Int? = null)
@Entity(foreignKeys = arrayOf(ForeignKey(entity = Usuario::class,
parentColumns = arrayOf("uid"),
childColumns = arrayOf("usuarioID"),
onDelete = ForeignKey.CASCADE)))
data class Medicamento (@PrimaryKey(autoGenerate = true) var id: Int,
var nombreMedicamento: String? = null,
var nombreGenerico: String? = null,
var dosis: String? = null,
var nota: String? = null,
var tipo: String? = null,
var color: Int? = null,
var fotografia: String? = null,
var usuarioID : Int? = null)
I did the join using DB Broser for SQLite and works fine:
SELECT Toma.id, statusToma, horaToma, nombreMedicamento, tipo, titulo, color, Tratamiento.id FROM TOMA JOIN Tratamiento ON Tratamiento.id = Toma.tratamientoID JOIN Medicamento ON Tratamiento.medicamentoID = Medicamento.id WHERE Tratamiento.usuarioID = 1
How can I solve this in Android using Room?
Maybe, try this. Update your query to include both the ids as different aliases.
@Query("SELECT Toma.id as tomaId, statusToma, horaToma, nombreMedicamento, tipo, titulo, color, Tratamiento.id as trataminetoId FROM TOMA JOIN Tratamiento ON Tratamiento.id = Toma.tratamientoID JOIN Medicamento ON Tratamiento.medicamentoID = Medicamento.id WHERE Tratamiento.usuarioID = :id")
fun getTomasDia(id: Int?) : LiveData<List<JoinTomasDelDia>>
And update your entity to have both the ids.
data class JoinTomasDelDia(
@ColumnInfo(name = "tomaId") val id: Int?,
@ColumnInfo(name = "trataminetoId") val id2: Int?,
@ColumnInfo(name = "horaToma") val horaToma: String?,
@ColumnInfo(name = "statusToma") var statusToma: Int?,
@ColumnInfo(name = "nombreMedicamento") val medicamento: String?,
@ColumnInfo(name = "tipo") val tipo: String?,
@ColumnInfo(name = "titulo") val tituloTratamiento: String?,
@ColumnInfo(name = "color") val color: Int?,
@ColumnInfo(name = "Tratamiento.id") val idTratamiento: Int?
)