Solution 1 :
As @BobSnyder said I’ve solved it by dropping the table first and the recreating the table. And it worked like a charm. So I’m putting his comment as answer in case anyone needs it in the future.
Problem :
I use Room persistence library for my database application. I’ve written a migration strategy to add a new table to existing database. I’m facing the following error.
Error:
java.lang.IllegalStateException: Migration didn't properly handle: table_audio_book(com.ridmik.app.audio_book.model.AudioBookInDb).
Expected:
TableInfo{name='table_audio_book', columns={book_path=Column{name='book_path', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, stream=Column{name='stream', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, user_id=Column{name='user_id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, audio_book_data=Column{name='audio_book_data', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, audio_book_id=Column{name='audio_book_id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, audio_book_name=Column{name='audio_book_name', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, stream_path=Column{name='stream_path', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, table_audio_book_row_id=Column{name='table_audio_book_row_id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, last_closing_time=Column{name='last_closing_time', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='table_audio_book', columns={user_id=Column{name='user_id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, audio_book_chapter_name=Column{name='audio_book_chapter_name', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, audio_book_data=Column{name='audio_book_data', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, audio_book_id=Column{name='audio_book_id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, audio_book_name=Column{name='audio_book_name', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, row_id=Column{name='row_id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, last_closing_time=Column{name='last_closing_time', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
My Model Class:
@Entity(tableName = "table_audio_book")
data class AudioBookInDb (
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "table_audio_book_row_id")
val rowId: Long,
@ColumnInfo(name = "audio_book_id")
val bookId: Int,
@ColumnInfo(name = "audio_book_name")
val bookName: String,
@ColumnInfo(name = "stream_path")
val streamPath: String,
@ColumnInfo(name = "book_path")
val bookPath: String,
@ColumnInfo(name = "stream")
val stream: Boolean,
@ColumnInfo(name = "audio_book_data")
val audioBookData: String,
@ColumnInfo(name = "user_id")
val userId: Int,
@ColumnInfo(name = "last_closing_time")
val lastClosingTime: Long
)
My Migration Snippet:
public static final Migration MIGRATION_6_7 = new Migration(6, 7) {
@Override
public void migrate(SupportSQLiteDatabase database) {
String createTableAudioBook = "CREATE TABLE IF NOT EXISTS `table_audio_book` (`table_audio_book_row_id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
" `audio_book_id` INTEGER NOT NULL, `audio_book_name` TEXT, `book_path` TEXT, `stream_path` TEXT, `stream` INTEGER NOT NULL, `audio_book_data` TEXT," +
" `user_id` INTEGER NOT NULL, `last_closing_time` INTEGER NOT NULL)";
database.execSQL(createTableAudioBook);
Timber.d("Upgrading Database from version 6 to 7");
}
};
Can anyone put me in right direction on how to solve this?
Comments
Comment posted by Bob Snyder
The “found table info” in the error message includes columns that do no exist in your migration statement to create the table, for example:
READ [ANSWERED] How to put an android layout's element over another one
Powered by Inline Related Posts
Comment posted by miq0717
@BobSnyder Thanks man. Worked like a charm! The problem was I when I created the table I used