Search code examples
sqljoinforeign-keysandroid-roomjunction

How to search two tables sharing a foreign key (I think I'm asking this right....)?


Dog entity

@Entity(tableName = "dog_table")
public class DogEntity {

  private int mId;
  private String mName, mBreed;
  etc..
}

Toy entity

@Entity(tableName = "toy_table")
public class ToyEntity {

  private int mId;
  private String mName, mBrand;
  etc..
}

DogAndToy join table entity

@Entity(tableName = "dog_and_toy_join_table",
    primaryKeys = {"mDogID", "mToyId"},
    foreignKeys = {
        @ForeignKey(
            entity = DogEntity.class,
            parentColumns = "mId",
            childColumns = "mDogID",
            onDelete = ForeignKey.CASCADE,
            onUpdate = ForeignKey.CASCADE
        ),
        @ForeignKey(
            entity = ToyEntity.class,
            parentColumns = "mId",
            childColumns = "mToyId",
            onDelete = ForeignKey.CASCADE,
            onUpdate = ForeignKey.CASCADE
        )
    },
    indices = {@Index("mDogID"), @Index("mToyId")}
)
public class DogAndToyJoinEntity{

  private final int mDogID, mToyId;

  public DogAndToyJoinEntity(int mDogID, int mToyId) {
    this.mDogID = mDogID;
    this.mToyId = mToyId;
  }
  etc..
}

DogAndToy data class

public class DogAndToy {

  @Embedded
  public Dog mDog;

  @Relation(
      parentColumn = "mId",
      entityColumn = "mId",
      entity = ToyEntity.class,
      associateBy =
      @Junction(
          value = DogAndToyJoinEntity.class,
          parentColumn = "mDogId",
          entityColumn = "mToyId"
      )
  )
  public List<ToyEntity> toyList;
}

notes: All dogs can have multiple toys, and toys can be associated with multiple dogs. Dog & Toy entities don't share any fields (eg - Dog doesn't have toyId, etc)

I've been trying for a few days to wrap my head around

how to query/get all dogs associated with one Toy (by name)

I use the DogAndToy data class for display purposes in my RecyclerView.

JOIN and INNER JOIN queries are baffling to me and I've been trying multiple variations but keep ending up with zero search results. Here's my most recent try:

  @Transaction
  @Query("SELECT dog_table.* FROM dog_table" +
      "INNER JOIN dog_and_toy_join_table ON dog_table.mId = dog_and_toy_join_table.mDogId" +
      "INNER JOIN toy_table ON toy_table.mId = dog_and_toy_join_table.mToyId " +
      "WHERE toy_table.mName LIKE :query")
  LiveData<List<DogAndToy>> findDogsByToyName(String query);

Can anyone suggest a step-by-step description of these queries in Android Room? Any of the JOIN articles/examples I find here or anywhere on the internets don't have a "join" (foreign key) reference... Am I even trying this in the right manner?

update: to clarify, I have FTS tables and my "basic" searches work fine (eg - search by name, etc)


Solution

  • Replace :toyName with the variable

    SELECT d.mName FROM dog_table AS d
    WHERE d.mId IN ( 
      SELECT j.mDogID FROM dog_and_toy_join_table AS j
      WHERE j.mToyId = (
        SELECT t.mId FROM toy_table AS t
        WHERE t.mName = :toyName));
    

    EDIT

    TBH, no idea why it only selects one row. Maybe someone else can answer it.

    It the mean time take this:

    select d.mName
    from dog_table d 
        INNER join dog_and_toy_join_table dt
            on d.mid = dt.mDogID 
        INNER JOIN toy_table t
            ON dt.mToyId = t.mId
        WHERE t.mName = 'toy1'