2

I am currently converting my projects to Kotlin and I have an app with Room database using Java.

My Entity in Java

@Entity(tableName = "store")
@Fts4
public class Store {

    @PrimaryKey
    @ColumnInfo(name = "rowid")
    private Long identification;

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

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

    @ColumnInfo(name = "days_open")
    private int daysOpen;

    public Store(Long identification, @NonNull String name, String location, int daysOpen) {
        this.identification = identification;
        this.name = name;
        this.location = location;
        this.daysOpen = daysOpen
    }

    public Long getIdentification() {
        return identification;
    }

    @NonNull
    public String getName() {
        return name;
    }

    public String getLocation() {
        return location;
    }

    public int getDaysOpen() {
        return daysOpen;
    }
}

I convert it this way to Kotlin

@Entity(tableName = "store")
@Fts4
data class Store(

    @PrimaryKey @ColumnInfo(name = "rowid") 
    val identification: Long?,

    @ColumnInfo(name = "name")
    val name: String,

    @ColumnInfo(name = "location")
    val location: String?

    @ColumnInfo(name = "days_open")
    val daysOpen: Int?
)

Now I am having this error

java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.
 

Do we really need to do migration in this? Or I am wrong converting things. I am using Room 2.3.0.

implementation "androidx.room:room-ktx:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"

When I updated the database version, this is the error

java.lang.IllegalStateException: A migration from 1 to 2 was required but not found. Please provide the necessary Migration path via RoomDatabase.Builder.addMigration(Migration ...) or allow for destructive migrations via one of the RoomDatabase.Builder.fallbackToDestructiveMigration* methods.
   

I added this code to my database

val MIGRATION_1_2 = object : Migration(1, 2) {
    override fun migrate(database: SupportSQLiteDatabase) {
        database.execSQL("
        // put changes here
        ")}
}

I don't know what to put inside of migrate function. Any idea?

Rence Rei
  • 473
  • 3
  • 9
  • This is just a guess but with location `String` and `String?` may not convert to the same thing as far as `room` is concerned. And since you most likely want to keep the option open for a null location I think the ! operator might work. `val location: String!` – avalerio Jun 20 '21 at 02:34
  • Thanks, but it's not working. It says Unexpected Token. – Rence Rei Jun 20 '21 at 03:05
  • That would be my guess too **if** you were using the wrong `@NonNull`. [See here](https://stackoverflow.com/a/47109367/506796). But there's no such thing as a `!` operator for type nullability. Try using `?` to make it a nullable String. – Tenfour04 Jun 20 '21 at 03:10
  • Oh, it looks like your `identification` Long field is a nullable Long in Java, so it needs to be `Long?` in Kotlin. Not sure if Room would treat a PrimaryKey as nullable though, even if you didn't specify `@NonNull` for it. – Tenfour04 Jun 20 '21 at 03:12
  • @Tenfour04 I am using correct '@NonNull'... import androidx.annotation.NonNull – Rence Rei Jun 20 '21 at 03:16
  • @Tenfour04 already did it. Still got error. Is it probably because of Fts4? – Rence Rei Jun 20 '21 at 03:21

2 Answers2

0

Exception message seems to be quite clear. you need to update the version of your room database. Go to the class that extends RoomDatabase and increment the value of version attribute in @Database annotation.

@Database(entities = [A::class, B::class], version = 2)
abstract class YourRoomDatabase: RoomDatabase() 
mightyWOZ
  • 7,946
  • 3
  • 29
  • 46
  • Already done this, it says I need to add the addMigration(Migration ...). But I don't know what to put here because I didn't change my schema, I just convert the language that I used. Any idea to put in AddMigration()? – Rence Rei Jun 20 '21 at 04:59
  • First update your question with exact error you are getting – mightyWOZ Jun 20 '21 at 05:09
0

Already got the solution.

The problem is my int in Java Entity is different w/ Int in Kotlin. Thus, I need to update my schema by Migration. My solution is referenced here.

Rence Rei
  • 473
  • 3
  • 9