Pre-packaged database has an invalid schema, order mixed up - android
I'm trying to implement this database with quite a few parameters. Unfortunately I cannot figure out why the order is mixed up that badly. The database is being read from the assets, it's already populated with ~250 items.
Error Message:
Expected:
TableInfo{name='Item', columns={tradingItem3Amount=Column{name='tradingItem3Amount', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCrafting2Amount=Column{name='itemCrafting2Amount', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, description=Column{name='description', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCrafting4Amount=Column{name='itemCrafting4Amount', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, buoyancy=Column{name='buoyancy', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, ItemCategory=Column{name='ItemCategory', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, tradingItem1Amount=Column{name='tradingItem1Amount', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCrafting3=Column{name='itemCrafting3', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCrafting4=Column{name='itemCrafting4', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCrafting1=Column{name='itemCrafting1', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCrafting2=Column{name='itemCrafting2', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCraftingAmount=Column{name='itemCraftingAmount', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1, defaultValue='null'}, durability=Column{name='durability', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, weight=Column{name='weight', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, friction=Column{name='friction', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCrafting3Amount=Column{name='itemCrafting3Amount', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, imgUrl=Column{name='imgUrl', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, tradingItem1=Column{name='tradingItem1', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, tradingItem2=Column{name='tradingItem2', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, tradingItem3=Column{name='tradingItem3', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, name=Column{name='name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, flammable=Column{name='flammable', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCrafting1Amount=Column{name='itemCrafting1Amount', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, tradingItem2Amount=Column{name='tradingItem2Amount', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, modpack=Column{name='modpack', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, extraInfo=Column{name='extraInfo', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, tradingAmount=Column{name='tradingAmount', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='Item', columns={Category=Column{name='Category', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, Description=Column{name='Description', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, tradingItem3Amount=Column{name='tradingItem3Amount', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, Name=Column{name='Name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, tradingItem1Amount=Column{name='tradingItem1Amount', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCrafting3=Column{name='itemCrafting3', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCrafting4=Column{name='itemCrafting4', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCrafting1=Column{name='itemCrafting1', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCrafting2=Column{name='itemCrafting2', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCraftingAmount=Column{name='itemCraftingAmount', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1, defaultValue='null'}, itemCraftingAmount1=Column{name='itemCraftingAmount1', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCraftingAmount2=Column{name='itemCraftingAmount2', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCraftingAmount3=Column{name='itemCraftingAmount3', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, itemCraftingAmount4=Column{name='itemCraftingAmount4', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, Durability=Column{name='Durability', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, Flammable=Column{name='Flammable', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, Bouyancy=Column{name='Bouyancy', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, ItemID=Column{name='ItemID', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, Weight=Column{name='Weight', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, tradingItem1=Column{name='tradingItem1', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, tradingItem2=Column{name='tradingItem2', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, tradingItem3=Column{name='tradingItem3', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, Friction=Column{name='Friction', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, tradingItem2Amount=Column{name='tradingItem2Amount', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, modpack=Column{name='modpack', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, extraInfo=Column{name='extraInfo', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, tradingAmount=Column{name='tradingAmount', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
The create statement:
This statement is copied from SQLite Browser
CREATE TABLE "Item" (
"id" INTEGER,
"Name" TEXT,
"ItemID" TEXT,
"Category" TEXT,
"Description" TEXT,
"Weight" INTEGER,
"Durability" INTEGER,
"Friction" INTEGER,
"Bouyancy" INTEGER,
"Flammable" INTEGER,
"itemCrafting1" TEXT,
"itemCraftingAmount1" INTEGER,
"itemCrafting2" TEXT,
"itemCraftingAmount2" INTEGER,
"itemCrafting3" TEXT,
"itemCraftingAmount3" INTEGER,
"itemCrafting4" TEXT,
"itemCraftingAmount4" INTEGER,
"extraInfo" TEXT,
"itemCraftingAmount" INTEGER,
"tradingAmount" INTEGER,
"tradingItem1" TEXT,
"tradingItem1Amount" INTEGER,
"tradingItem2" TEXT,
"tradingItem2Amount" INTEGER,
"tradingItem3" TEXT,
"tradingItem3Amount" INTEGER,
"modpack" TEXT,
PRIMARY KEY("id")
)
Item.kt
package com.blazecode.scrapguidev2.ui.items
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.blazecode.scrapguidev2.ui.items.enums.Modpack
import com.blazecode.scrapguidev2.ui.items.enums.ItemCategory
#Entity
data class Item(
#PrimaryKey var id: String = "0",
#ColumnInfo(name = "name") var name: String?,
#ColumnInfo(name = "imgUrl") var imgUrl: Int?,
#ColumnInfo(name = "ItemCategory") var category: ItemCategory?,
#ColumnInfo(name = "description") var description: String?,
#ColumnInfo(name = "weight") var weight: Int?,
#ColumnInfo(name = "durability") var durability: Int?,
#ColumnInfo(name = "friction") var friction: Int?,
#ColumnInfo(name = "buoyancy") var buoyancy: Int?,
#ColumnInfo(name = "flammable") var flammable: Boolean?,
#ColumnInfo(name = "itemCrafting1") var itemCrafting1: String?,
#ColumnInfo(name = "itemCrafting2") var itemCrafting2: String?,
#ColumnInfo(name = "itemCrafting3") var itemCrafting3: String?,
#ColumnInfo(name = "itemCrafting4") var itemCrafting4: String?,
#ColumnInfo(name = "itemCrafting1Amount") var itemCrafting1Amount: Int?,
#ColumnInfo(name = "itemCrafting2Amount") var itemCrafting2Amount: Int?,
#ColumnInfo(name = "itemCrafting3Amount") var itemCrafting3Amount: Int?,
#ColumnInfo(name = "itemCrafting4Amount") var itemCrafting4Amount: Int?,
#ColumnInfo(name = "extraInfo") var extraInfo: String?,
#ColumnInfo(name = "itemCraftingAmount") var itemCraftingAmount: Int?,
#ColumnInfo(name = "tradingAmount") var tradingAmount: Int?,
#ColumnInfo(name = "tradingItem1") var tradingItem1: String?,
#ColumnInfo(name = "tradingItem1Amount") var tradingItem1Amount: Int?,
#ColumnInfo(name = "tradingItem2") var tradingItem2: String?,
#ColumnInfo(name = "tradingItem2Amount") var tradingItem2Amount: Int?,
#ColumnInfo(name = "tradingItem3") var tradingItem3: String?,
#ColumnInfo(name = "tradingItem3Amount") var tradingItem3Amount: Int?,
#ColumnInfo(name = "modpack") var modpack: Modpack?
)
The order is irrelevant, it is not an issue. From a very quick inspection (of the generated SQL - see below) you have an issue with NOT NULL being omitted for the id column. However as explained below comparison/adjustment isn't required as Room generates the exact SQL for the table(s).
The simple way to ascertain what Room expects (demands in reality) is to
Make the required changes to the #Entity annotated classes.
Include the #Entity annotated classes in the #Database annotated class.
Compile the project (CTRL + F9)
Using the Android View of Android Studio go to the java(generated) and expand it to locate the class that is the same name as the #Database annotated class but suffixed with _Impl.
Locate the createAllTables method and you will see the exact SQL for the creation of the table(s).
Ignore the room_master_table Room will ensure that this is created and populated.
The following is an example where your code has been added to an existing project (used for other answers):-
Related
Room migration with UNIQUE KEY constraint not working
Error: java.lang.IllegalStateException: Migration didn't properly handle: insta_alerts(org.altruist.BajajExperia.Models.CEInstaViewDetailsDTO). Expected: TableInfo{name='insta_alerts', columns={isSeen=Column{name='isSeen', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, smsText=Column{name='smsText', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, sentDate=Column{name='sentDate', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, campaign=Column{name='campaign', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, customerId=Column{name='customerId', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1, defaultValue='null'}, header=Column{name='header', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, priority=Column{name='priority', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, notificationText=Column{name='notificationText', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, alertMessage=Column{name='alertMessage', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[Index{name='index_insta_alerts_smsText_customerId', unique=true, columns=[smsText, customerId]}]} Found: TableInfo{name='insta_alerts', columns={isSeen=Column{name='isSeen', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, smsText=Column{name='smsText', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, sentDate=Column{name='sentDate', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, campaign=Column{name='campaign', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, customerId=Column{name='customerId', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1, defaultValue='null'}, header=Column{name='header', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, priority=Column{name='priority', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, notificationText=Column{name='notificationText', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, alertMessage=Column{name='alertMessage', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]} Query: database.execSQL("CREATE TABLE IF NOT EXISTS insta_alerts(id INTEGER PRIMARY KEY,campaign TEXT,notificationText TEXT,header TEXT,alertMessage TEXT,priority TEXT,sentDate TEXT,smsText TEXT,customerId TEXT,isSeen INTEGER, CONSTRAINT index_insta_alerts_smsText_customerId UNIQUE(smsText, customerId) ON CONFLICT REPLACE)") Model: #Entity( tableName = "insta_alerts", indices = [Index( value = ["smsText", "customerId"], unique = true )] ) #Parcelize data class CEInstaViewDetailsDTO( #PrimaryKey(/*autoGenerate = true*/) #ColumnInfo(name = "id") #SerializedName("id") var id: Long? = null, #ColumnInfo(name = "campaign") #SerializedName("campaign") var campaign: String? = null, #ColumnInfo(name = "notificationText") #SerializedName("notificationText") var notificationText: String? = null, #ColumnInfo(name = "header") #SerializedName("header") var header: String? = null, #ColumnInfo(name = "alertMessage") #SerializedName("alertMessage") var alertMessage: String? = null, #ColumnInfo(name = "priority") #SerializedName("priority") var priority: String? = null, #ColumnInfo(name = "sentDate") #SerializedName("sentDate") var sentDate: String? = null, #ColumnInfo(name = "smsText") #SerializedName("smsText") var smsText: String? = null, #ColumnInfo(name = "customerId") #SerializedName("customerId") var customerId: String? = null, #ColumnInfo(name = "isSeen") #SerializedName("isSeen") var isSeen: Int? = null ) : Parcelable
You should add your index creation to your migration like database.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS index_insta_alerts_smsText_customerId ON insta_alerts (smsText,customerId)")
Room is very inconvinient... what's ther problem?
I'v got the error Caused by: java.lang.IllegalStateException: Migration didn't properly handle memolist(com.jakchang.savelocation.Entity.MemoEntity). Expected: TableInfo{name='memolist', columns={date=Column{ name='date', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, fontType=Column{name='fontType', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, address=Column{name='address', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, nation=Column{name='nation', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, city=Column{name='city', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, latitude=Column{name='latitude', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, title=Column{name='title', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, uri2=Column{name='uri2', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, uri3=Column{name='uri3', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, uri1=Column{name='uri1', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, isDeleted=Column{name='isDeleted', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, tag=Column{name='tag', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, text=Column{name='text', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, uri4=Column{name='uri4', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, longitude=Column{name='longitude', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]} Found: I think I wrote right way the entity.. but I got the error. I'm using livedata with mvvm. Please Let me know to solve this.... Entity #Entity(tableName = "memolist") public class MemoEntity { #PrimaryKey(autoGenerate = true) #NonNull int id; #ColumnInfo(name ="latitude") String latitude; #ColumnInfo(name ="longitude") String longitude; #ColumnInfo(name ="nation") String nation; #ColumnInfo(name ="city") String city; #ColumnInfo(name ="address") String address; #ColumnInfo(name ="title") String title; #ColumnInfo(name ="tag") String tag; #ColumnInfo(name ="date") String date; #ColumnInfo(name ="uri1") String uri1; #ColumnInfo(name ="uri2") String uri2; #ColumnInfo(name ="uri3") String uri3; #ColumnInfo(name ="uri4") String uri4; #ColumnInfo(name ="text") String text; #ColumnInfo(name ="fontType") String fontType; #ColumnInfo(name ="isDeleted") String isDeleted; public MemoEntity(){} getter/setter~~ } is it important to match the below tableinfo sequence and entity sequence? hard to study alone.... really I wanna solve this...
Check the error message and especially the differences between excepted and found. It can be an issue with a default value in your migration script. For example, using the Room 2.1.0 and adding a new integer in my Entity, I have set a default value : object MIGRATION_1_2 : Migration(1, 2) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE entity ADD COLUMN newInteger INTEGER NOT NULL DEFAULT 0") } }
Room cannot migrate database
My Entity #Entity(tableName = "user_table") data class User( val userID:String, val userName:String, val userRoleName:String, val adminID:String, val name:String, val email:String, val phoneNumber:String, val adminType:String, val batchID:String, val centerID:String, val batchName:String, val centerName:String, val password:String ) { #PrimaryKey(autoGenerate = true) var id: Int = 0 } I added the password column; The migration that I provided is private val migration: Migration = object : Migration(1, 2) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE user_table ADD COLUMN password TEXT") } } I have used it here private fun buildDB(c: Context): MDatabase { val dbName = "lms_database.db" return Room.databaseBuilder(c.applicationContext, MDatabase::class.java, dbName) .addMigrations(migration) .build() } I am getting a lengthy error: java.lang.IllegalStateException: Migration didn't properly handle: user_table(app.database.entities.User). Expected: TableInfo{name='user_table', columns={batchName=Column{name='batchName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, centerID=Column{name='centerID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, adminType=Column{name='adminType', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, userName=Column{name='userName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, batchID=Column{name='batchID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, userID=Column{name='userID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, password=Column{name='password', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, phoneNumber=Column{name='phoneNumber', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, adminID=Column{name='adminID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, name=Column{name='name', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, email=Column{name='email', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, userRoleName=Column{name='userRoleName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, centerName=Column{name='centerName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]} Found: TableInfo{name='user_table', columns={batchName=Column{name='batchName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, centerID=Column{name='centerID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, adminType=Column{name='adminType', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, userName=Column{name='userName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, batchID=Column{name='batchID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, userID=Column{name='userID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, password=Column{name='password', type='Text', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, phoneNumber=Column{name='phoneNumber', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, adminID=Column{name='adminID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, name=Column{name='name', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, email=Column{name='email', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, userRoleName=Column{name='userRoleName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, centerName=Column{name='centerName', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]} at androidx.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:103) at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade(FrameworkSQLiteOpenHelper.java:124) The database already has some data right now.
You have to set password nullable in entity model or provide non null value during migration. Try setting Either val password:String? Or ALTER TABLE user_table ADD COLUMN password TEXT NOT NULL DEFAULT '' N.B: Replace empty with your default one
Migration not working in boolean value in Room Android
I got it below mentioned exception Migration didn't properly handle Expected: TableInfo{name='ContactModel', columns={name=Column{name='name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, number=Column{name='number', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1}, isVerified=Column{name='isVerified', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, age=Column{name='age', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]} Found: TableInfo{name='ContactModel', columns={number=Column{name='number', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1}, isVerified=Column{name='isVerified', type='Boolean', affinity='1', notNull=false, primaryKeyPosition=0}, name=Column{name='name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, age=Column{name='age', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}}, foreignKeys=[], indices=[]} Query val migration2 = object : Migration(2, 3) { override fun migrate(database: SupportSQLiteDatabase) { val migString = "ALTER TABLE ContactModel ADD COLUMN isVerified Boolean" database.execSQL(migString) } }
I had the same issue and it looks like Room does not support Boolean, so your migration string should be: val migString = "ALTER TABLE ContactModel ADD COLUMN isVerified INTEGER NOT NULL DEFAULT 0" and you can then use it as boolean in your code.
Room Database Migration Issue with Unique Index
I was trying to upgrade my Database version and in last version the table had a unique index created upon migration room gave me the following error Expected: TableInfo{name='ALERT_STATUS', columns={resolution_time=Column{name='resolution_time', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, status=Column{name='status', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1}, event_time=Column{name='event_time', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, is_uploaded=Column{name='is_uploaded', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, InvErrorCode=Column{name='InvErrorCode', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, is_fault=Column{name='is_fault', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, FaultDetails=Column{name='FaultDetails', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, code=Column{name='code', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, component=Column{name='component', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[Index{name='index_ALERT_STATUS_code_event_time', unique=true, columns=[code, event_time]}]} Found: TableInfo{name='ALERT_STATUS', columns={resolution_time=Column{name='resolution_time', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, status=Column{name='status', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1}, event_time=Column{name='event_time', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, is_uploaded=Column{name='is_uploaded', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, InvErrorCode=Column{name='InvErrorCode', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, is_fault=Column{name='is_fault', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, FaultDetails=Column{name='FaultDetails', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, code=Column{name='code', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, component=Column{name='component', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[Index{name='code_time', unique=true, columns=[code, event_time]}]} I tried removing the unique index and the migration worked fine but with unique index it never passed My database Entity is below #Entity(tableName = "ALERT_STATUS" , indices = {#Index(value = {"code", "event_time"}, unique = true)}) public class AlertStatus implements Serializable{ #PrimaryKey(autoGenerate = true) public Integer id; #NonNull public String event_time; public String resolution_time; #NonNull public String code; public Integer component; public int status; public int is_fault; public String FaultDetails; public String InvErrorCode; public int is_uploaded; }
Try this worked for me. Create a new Table i.e. Holder table which will hold your data between structural changes. database.execSQL("CREATE TABLE TABLE_NAME (id INTEGER, column1 varchar(25), column2 varchar(25) , PRIMARY KEY(id))"); Create Unique Index as required database.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS <index-name> ON TABLE_NAME (column1,column2)");