Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: An entity must have at least 1 field annotated with @PrimaryKey

Tags:

android

I am new to using room persistence and I have this error whenever I try running my code. It shows no error when editing but I get build error on gradle. It failed me that I had to copy paste codes but none seemed to work. The error is below

error: An entity must have at least 1 field annotated with @PrimaryKey

How do I work it out? My code is below;

 package com.revosleap.dummy.DatabaseMov;

import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.PrimaryKey;

@Entity

public class TodoListItem {
    @Ignore
    @PrimaryKey(autoGenerate = true)
     private int id;

    @ColumnInfo(name = "time")
    private String time;

    @ColumnInfo(name="title")
    private String title;

    public TodoListItem(){

    }

    public TodoListItem(String time, String title) {
        this.time = time;
        this.title = title;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}
like image 394
Carlos Anyona Avatar asked Sep 16 '25 12:09

Carlos Anyona


2 Answers

You shouldn't have primary key as private or static

Change private int id; this line to

@PrimaryKey
public int id;

more details Room entities

like image 73
Ercan Avatar answered Sep 18 '25 02:09

Ercan


You should not use @Ignore with @PrimaryKey

@Ignore annotation Ignores the marked element from Room's processing logic.

like image 37
Farhad Navayazdan Avatar answered Sep 18 '25 02:09

Farhad Navayazdan