Dak28
Dak28

Reputation: 259

Primary key is not increase room android

I'm creating simple database with student and classes.

I created both entities and many to many relation with view models.

But, I notice that id of both is not incremented.

@Entity(tableName = "students")
class Student(
    val name: String,
    val lastName: String
) {
    @PrimaryKey(autoGenerate = true) var id: Int = 0
}

this is the result on table creation:

        val student = Student("Daniele", "[email protected]")
        val student2 = Student("Daniele", "[email protected]")

        Log.d("test", "student Id : ${student.id}")
        Log.d("test", "student Id : ${student2.id}")
        student Id : 0 student Id : 0

Upvotes: 2

Views: 1218

Answers (2)

MikeT
MikeT

Reputation: 57103

The code :-

    val student = Student("Daniele", "[email protected]")
    val student2 = Student("Daniele", "[email protected]")

    Log.d("test", "student Id : ${student.id}")
    Log.d("test", "student Id : ${student2.id}")
    student Id : 0 student Id : 0

Is not getting anything from the database. It is creating the 2 Student objects (so id is 0) and printing them.

You should be inserting the Students into the database and to test them extracting them via the functions defined as part of the respective @Dao class.

e.g assuming a StudentDao interface as :-

@Dao
interface StudentDao {

    @Insert
    fun insert(student: Student) :Long

    @Query("SELECT * FROM students")
    fun getAllStudents() :List<Student>
}
  • Allows a student to be inserted and a List of of Students to be extracted
  • Note that the Database class needs to include entities = {Student.class} in the @Database and also include abstract StudentDao studentDao();

Then after building the Database, in this case into a Database object namd mDB you can use (note the build uses .allowMainThreadQueries and also the following is in Java for convenience) :-

mDB.studentDao().insert(new Student("Fred","Bloogs"));
mDB.studentDao().insert(new Student("Mary","Smith"));
List<Student> studentList = mDB.studentDao().getAllStudents();
for (Student s: studentList) {
    Log.d("STUDENTINFO", "ID=" + s.getId() + " Name = " + s.getName() + " " + s.getLastName());
}

i.e. Adds 2 Students to the database and then extracts the students resulting in :-

2019-11-29 00:30:15.568 D/STUDENTINFO: ID=1 Name = Fred Bloogs
2019-11-29 00:30:15.568 D/STUDENTINFO: ID=2 Name = Mary Smith

I believe that the equivalent of the above code in Kotlin would be something like :-

    mDB.StudentDao().insert(Student("Fred","Bloggs"))
    mDB.StudentDao().insert(Student("Mary","Smith"))
    val studentsList = mdb.StudentDao().getAllStudents()
    for (s :Student in studentsList) {
        Log.d("STUDENTINFO","ID= ${s.id} Name = ${s.name} ${s.lastName}" )
    }

Upvotes: 4

kaushal malvi
kaushal malvi

Reputation: 72

Use below code it will solve your problem.

@Entity(tableName = "students")
data class Student(
    @PrimaryKey(autoGenerate = true) 
    var id: Int,
    val name: String,
    val lastName: String
)

Upvotes: -1

Related Questions