Search code examples
androidandroid-room

Get all correct values from query, except for id


I created an entity like this (I removed some properties, but the structure is the same).

@Entity(tableName = "circuits")
public class Circuit {

    @PrimaryKey(autoGenerate = true)
    private int id;
    private String name;

    public Circuit(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Ignore
    public Circuit(String name) {
        this.name = name;
    }

    @Ignore
    public Circuit() {}

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

@Entity(tableName = "circuit_sessions")
public class CircuitSession {

    @PrimaryKey(autoGenerate = true)
    private int id;
    private int circuitId;
    private int driverId;

    public CircuitSession(int id, int circuitId, int driverId) {
        this.id = id;
        this.circuitId = circuitId;
        this.driverId = driverId;
    }

    @Ignore
    public CircuitSession(int circuitId, int driverId) {
        this.circuitId = circuitId;
        this.driverId = driverId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getCircuitId() {
        return circuitId;
    }

    public void setCircuitId(int circuitId) {
        this.circuitId = circuitId;
    }

    public int getDriverId() {
        return driverId;
    }

    public void setDriverId(int driverId) {
        this.driverId = driverId;
    }
}

Then I have a query like this in my DAO.

@Query("SELECT * FROM circuits INNER JOIN circuit_sessions ON circuits.id = circuit_sessions.circuitId WHERE circuit_sessions.driverId = :driverId ORDER BY name ASC")
    LiveData<List<Circuit>> getCircuitsWithOwnSessions(int driverId);

When I call this query it correctly returns one Circuit object and it is the circuit, which I expect it to be. But when I use the getId() function of that Circuit object it returns 1 instead of 2, which is the correct circuit id. All the other properties are correct and the expected values. Just the id is wrong.

When I check the App Inspection in Android Studio and run the query there, it shows the same circuit and it shows, the correct id, which is 2.

So, what could cause the wrong id when I call the query in my code? Is there a way to make sure, that I always get the id, which is saved in the database?


Solution

  • MikeT's answer pointed me in the right direction. The problem was, that both tables have an id column. I resolved that by changing the query to this:

    @Query("SELECT circuits.* FROM circuits INNER JOIN circuit_sessions ON circuits.id = circuit_sessions.circuitId WHERE circuit_sessions.driverId = :driverId ORDER BY name ASC")
        LiveData<List<Circuit>> getCircuitsWithOwnSessions(int driverId);