Valckef
Valckef

Reputation: 63

Primary key does not exist in the entity in android room with composite key

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();
}

Upvotes: 4

Views: 3255

Answers (1)

Domin
Domin

Reputation: 1145

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.

Upvotes: 7

Related Questions