Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
}
like image 729
Valckef Avatar asked Oct 20 '25 04:10

Valckef


1 Answers

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.

like image 65
Domin Avatar answered Oct 21 '25 17:10

Domin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!