4

The error I am getting is:

error: theme_id, picture_id referenced in the primary key does not exists in the Entity. Available column names:theme_id, picture_id, image

I have looked extensively online but i cannot find anything about this. This error only seemst to pop uw when trying to use the composite key. If I annotate one of the fields with a normal PrimaryKey it works just fine. I do not understand what is going on here and it is frustrating that i cannot find anything online about this. I hope you guys will be able to help me.

Entity

@Entity(primaryKeys = {"theme_id, picture_id"}, tableName = "picture")
public class Picture {

    @ColumnInfo(name = "theme_id")
    private int themeId;
    @ColumnInfo(name = "picture_id")
    private int pictureId;
    @ColumnInfo(name = "image", typeAffinity = ColumnInfo.BLOB)
    private byte[] image;
}

Dao

@Dao
public interface PictureDao {

    @Insert
    void instertPictures(Picture... pictures);

    @Update
    void updatePictures(Picture... pictures);

    @Delete
    void deletePictures(Picture... pictures);

    @Query("SELECT * FROM picture")
    List<Picture> getAllPictures();

    @Query("SELECT * FROM picture WHERE theme_id = :themeId")
    List<Picture> getThemePictures(int themeId);

    @Query("SELECT * FROM picture WHERE theme_id = :themeId AND picture_id = :pictureId")
    Picture getPicture(int themeId, int pictureId);

}

Database

@Database(entities = {Picture.class}, version = 1, exportSchema = false)
public abstract class PictureDatabase extends RoomDatabase {

    public static final String NAME = "picture_db";

    public abstract PictureDao pictureDao();
}
Valckef
  • 63
  • 1
  • 6
  • Possible duplicate of [How to make composite key in Room while using MVVM in android](https://stackoverflow.com/questions/47130307/how-to-make-composite-key-in-room-while-using-mvvm-in-android) – Bö macht Blau Dec 11 '18 at 20:18

1 Answers1

7

You get this error, because you haven't created column named theme_id, picture_id. Probably you meant to have 2 primary keys theme_id and picture_id. Then you have to pass two strings separated with comma, not one with comma inside.

So change

{"theme_id, picture_id"}
 "  only one string   "

to

{"theme_id", "picture_id"}
 " first  ", "  second  "
 " string "  "  string  "

and it should work just fine.

Boken
  • 4,825
  • 10
  • 32
  • 42
Domin
  • 1,075
  • 1
  • 11
  • 28
  • Hey thanks a ton for you answer. I don't know how I overlooked this. – Valckef Dec 12 '18 at 16:19
  • Just want to say that I havent actually tested your fix. I just suppose it will work. I got it to work with public staic final int fields in there. but Its the same solution actually. – Valckef Dec 12 '18 at 16:20