Felipe Pereira Garcia
Felipe Pereira Garcia

Reputation: 94

Error creation new table in Room database

I´m creating a new table in Room but I have a error with the migration. The problem is in favoriteId, there is a different between what is expected (primaryKeyPosition=1) and what is found in (primaryKeyPosition=0). If I delete data, it works fine.

java.lang.IllegalStateException: Migration didn't properly handle: favoriteRetailer(com.tiendeo.core.data.model.local.favorite.FavoriteRetailerLocalEntity). Expected: TableInfo{name='favoriteRetailer', columns={lon=Column{name='lon', type='REAL', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}, retailerId=Column{name='retailerId', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, countryCode=Column{name='countryCode', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, favoriteId=Column{name='favoriteId', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1, defaultValue='null'}, city=Column{name='city', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, retailerName=Column{name='retailerName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, type=Column{name='type', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, lat=Column{name='lat', type='REAL', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]} Found: TableInfo{name='favoriteRetailer', columns={lon=Column{name='lon', type='REAL', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}, retailerId=Column{name='retailerId', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, countryCode=Column{name='countryCode', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, favoriteId=Column{name='favoriteId', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, city=Column{name='city', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, retailerName=Column{name='retailerName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, type=Column{name='type', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, lat=Column{name='lat', type='REAL', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}

This is my model and the migration

@Entity(tableName = "favoriteRetailer")
class FavoriteRetailerLocalEntity(
    @PrimaryKey var favoriteId: String,
    var city: String?,
    var lat: Float,
    var lon: Float,
    var retailerName: String,
    var retailerId: String,
    var countryCode: String,
    var type: Int
val TIENDEO_DB_MIGRATION_4_5: Migration = object : Migration(4, 5) {
  override fun migrate(database: SupportSQLiteDatabase) {
    try{
      database.execSQL(
          "CREATE TABLE IF NOT EXISTS 'favoriteRetailer' ('favoriteId' TEXT NOT NULL, 'city' TEXT,  'lat' REAL NOT NULL, 'lon' REAL NOT NULL, 'retailerName' TEXT NOT NULL, 'retailerId' TEXT NOT NULL, 'countryCode' TEXT NOT NULL, 'type' INTEGER NOT NULL)"
      )
    } catch (e: java.lang.RuntimeException) {
      Log.i("Error", "error: $e. Problem with migration 4_5")
    }
  }
}

Upvotes: 0

Views: 1108

Answers (2)

Felipe Pereira Garcia
Felipe Pereira Garcia

Reputation: 94

I found the solution. The problem was in the definition of table. I had to put PRIMARY KEY('id') in the of create table

Upvotes: 0

Muhammad Adil
Muhammad Adil

Reputation: 42

The problem is with your model class.

    @Entity(tableName = "favoriteRetailer")
class FavoriteRetailerLocalEntity(
    @PrimaryKey var favoriteId: String,
    var city: String?,
    var lat: Float,
    var lon: Float,
    var retailerName: String,
    var retailerId: String,
    var countryCode: String,
    var type: Int

the city is nullable but Room expected it to be not null.so change it like this

 @Entity(tableName = "favoriteRetailer")
    class FavoriteRetailerLocalEntity(
        @PrimaryKey var favoriteId: String,
        var city: String,
        var lat: Float,
        var lon: Float,
        var retailerName: String,
        var retailerId: String,
        var countryCode: String,
        var type: Int

Upvotes: 0

Related Questions