Search code examples
androidandroid-roomandroid-architecture-components

java.lang.IllegalStateException: Migration didn't properly handle table


How do you migrate an empty field type to text in Room ?

Right now I'm facing this issue :

java.lang.IllegalStateException: Migration didn't properly handle data_table

Expected: TableInfo{name='data_table', columns= url=Column{name='url', type='TEXT', notNull=false, primaryKeyPosition=0}.....

Found: TableInfo{name='data_table', columns= url=Column{name='url', type='', notNull=false, primaryKeyPosition=0}.....

I've tried using the UNDEFINED typeAffinity, but that has no effect.


Solution

  • The problem is when creating the table, the creator did't indicate the column explicitly. There are two ways to be chosen to solve the problem.

    1. You can create a new DB during migration and copy all the old data into the new one. Like this.

      static final Migration MIGRATION_1_2 = new Migration(1, 2) {
          @Override
          public void migrate(SupportSQLiteDatabase database) {
              // Create the new table
              database.execSQL(
                      "CREATE TABLE data_table_new (url TEXT");
              // Copy the data
              database.execSQL(
                      "INSERT INTO data_table_new url SELECT url FROM data_table");
              // Remove the old table
              database.execSQL("DROP TABLE data_table");
              // Change the table name to the correct one
              database.execSQL("ALTER TABLE data_table_new RENAME TO data_table");
          }
      };
      

    But it is inefficient when working with large db. So I use the second way.

    2 You migrate the table to new table with explicit column type with your favourite db manager or terminal. Then put the new db into your project.