title: Room composite Primary Key link to Foreign Key
body: <p>I am implementing an android application with Room database and have a small question about relationships in this DB.</p>
  
  <p>I have two tables:</p>
  
  <pre><code>@Entity(tableName = "foods", primaryKeys = {"id", "language_id"},
          indices = {@Index(value = {"id", "language_id"}, unique = true)},
          inheritSuperIndices = true)
  public class Food {
  
      @NonNull
      @ColumnInfo(name = "id")
      private String mId;
  
      @NonNull
      @ColumnInfo(name = "language_id")
      private String mLanguageId;
  
  }
  
  @Entity(tableName = "ingredients", primaryKeys = {"id", "language_id"},
          indices = {@Index(value = {"id", "language_id"}, unique = true), 
          @Index(value = {"food_id", "food_language_id"}, unique = true)},
          foreignKeys = {@ForeignKey(entity = Food.class, parentColumns ="id", 
          childColumns = "food_id", onUpdate = CASCADE, onDelete = CASCADE),
          @ForeignKey(entity = Food.class, parentColumns = "language_id", 
          childColumns = "food_language_id", onUpdate = CASCADE, onDelete = 
          CASCADE)},
          inheritSuperIndices = true)
  public class Ingredient {
  
      @NonNull
      @ColumnInfo(name = "id")
      private String mId;
  
      @NonNull
      @ColumnInfo(name = "language_id")
      private String mLanguageId;
  
      @ColumnInfo(name = "food_id")
      private String mFoodId;
  
      @ColumnInfo(name = "food_language_id")
      private String mFoodLanguageId;
  
  }
  </code></pre>
  
  <p>The both tables 'Food' and 'Ingredient' have composite primary keys ('id', 'language_id'). The Food object has to contains a List and of course   a @Relationship</p>
  
  <pre><code>public class FoodWithIngredients extends Food{
  
      @Relation(parentColumn = "id", entityColumn = "food_id", entity = 
      Ingredient.class)
      private List&lt;Ingredient&gt; mIngredients;
  
  }
  </code></pre>
  
  <p>After I try to run this code received these messages</p>
  
  <p><strong>Warnning:</strong></p>
  
  <blockquote>
    <p>food_language_id column references a foreign key but it is not part of
    an index. This may trigger full table scans whenever parent table is
    modified so you are highly advised to create an index that covers this
    column.</p>
  </blockquote>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>Ingredient has a foreign key (food_id) that references Food (id) but Food does not have a unique index on those columns nor the columns are its primary key. SQLite requires having a unique constraint on referenced parent columns so you must add a unique index to Food that has (id) column(s).
  </code></pre>
  
  <p>Could someone help me with that?</p>
  
  <p>Thanks in advance :)</p>