RoomAsset
RoomAsset copied to clipboard
Migration didn't properly handle <table_name>
I have a stored sqlite file from an old android app that I am putting into a new android app. Part of the upgrade process I am upgrading it to use Room. I found this because I needed it to read from an existing file. The problem is, every time I try to run the app in an emulator I am getting the Migration failed error. Here is the error...
java.lang.IllegalStateException: Migration didn't properly handle vegetables_table(com.androidtests.roomdemo.models.Vegetable).
Expected:
TableInfo{name='vegetables_table', columns={grams_in_cup=Column{name='grams_in_cup', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, name=Column{name='name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1}, long_desc=Column{name='long_desc', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, com_name=Column{name='com_name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='vegetables_table', columns={}, foreignKeys=[], indices=[]}
However, I can open the database in DB Browser for SQLite and it shows all of the columns and data in the vegetables_table schema, You can see from the screenshot that all of the columns are there. This is the exact file that is in my android project, but RoomAsset is saying there are no columns in the database, even though there are columns and data in the database. Any ideas?
I am also facing this same issue when trying to fetch data from any table. Pleas provide some solution if possible. Thanks in advance.
Caused by: java.lang.IllegalStateException: Migration didn't properly handle master_outlet
This is not an issue, you just didn't correctly format your Entity Table!
It must have the exact same Fields and configurations as the Database you want to import!
Take a look here: Its teeling you what it needs
Expected: TableInfo{name='vegetables_table', columns={grams_in_cup=Column{name='grams_in_cup', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, name=Column{name='name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1}, long_desc=Column{name='long_desc', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, com_name=Column{name='com_name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
Set this table up as Room Entity and you'll be fine.
add filed into database with default value when its notNull=true example ALTER TABLE TABLE_NAME ADD COLUMN newFiled INTEGER NOT NULL DEFAULT 0
I had the same issue but the above comment maybe is the CORRECT ANSWER. Really appreciatable. Thank you.
What happens is that you changed your table by adding fields or by removing or changing the name and time of doing the migration gave this problem, without putting the changes in the table. But he hopes that his table was in the right expectation, so he found different what he expected, I decided like this:
val MIGRATION_2_3 = object: Migration (2, 3) {
override fun migrate (database: SupportSQLiteDatabase) {
database.execSQL ("" "
CREATE TABLE new_Song (
id INTEGER PRIMARY KEY NOT NULL,
name TEXT,
TEXT NOT NULL DEFAULT tag ''
)
"" ".trimIndent ())
database.execSQL ("" "
INSERT INTO new_Song (id, name, tag)
SELECT id, name, tag FROM Song
"" ".trimIndent ())
database.execSQL ("DROP TABLE Song")
database.execSQL ("ALTER TABLE new_Song RENAME TO Song")
}
}
At the end of the page teaches the reason for the official website of Room: https://developer.android.com/training/data-storage/room/migrating-db-versions?hl=en#kotlin