22

What i found out so far

All the @entity annotated classes are processed during compiletime and an Implementation for Database class is generated. Then before accessing the db, validateMigration method of this generated class is called. This validateMigration method verifies with the existing db schema via raw query

PRAGMA table_info mytable name

(see L208 of android.arch.persistence.room.util.TableInfo.java)

Now the problem

My sqlite3 db has some columns with column type as BOOLEAN. (which slqite internally handles to int). Now when i create room entities say

public someEntity {
     @columnInfo(name="someName")
     public Boolean myValue;
}

The room's create table query will be

Create Table someEntity ( myValue INTEGER)

Where as when we query the existing db with PRAGMA table_info someEntity we get

1|myValue|BOOLEAN|0||0

As explained above room verifies the ( sqlite to room ) migration by comparing field name, column type etc. And since the column types dont match (BOOLEAN and INTEGER) it throws an error saying migration failed.

Can anyone suggest a workaround to this ? Can we make room create BOOLEAN column type in sqlite ? (Also afaik we can't change/alter column types of existing tables.)

PS: I also see a similar issue with VARCHAR - Using an existing VARCHAR column with Room

5
  • 1
    issuetracker.google.com/issues/71953987 Commented Jan 15, 2018 at 11:59
  • 1
    Thanks for the link. I came across issuetracker.google.com/issues/64822502 posted by you on the other question i mentioned. Looks like the issue has been there since august '17. Any workaround you can suggest other than creating a new table ? @CommonsWare
    – BruceWayne
    Commented Jan 15, 2018 at 17:01
  • 1
    "Also afaik we can't change/alter column types of existing tables" -- you can create a temp table with your existing structure, copy the data to it, drop the real table, rebuild the real table with the Room-approved types, then copy the data back over. That's the only workaround that I know of at the present time. Commented Jan 15, 2018 at 17:04
  • @CommonsWare I am facing this issue with copying the data from sqlite to room table using migration. When I do not add not null to the create query inside migration, the create query and the entity with boolean/ int do not match and throwing error. If I add not null in the create query, I am getting error "android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed:" . I have cross checked the old table and none of these columns have null value. They are all filled with either default values or inserted values. Any suggesstions what I might be doing wrong? Commented Apr 13, 2020 at 18:17
  • @AshishKanswal: All that I can suggest is that you ask a separate Stack Overflow question where you provide a minimal reproducible example. Commented Apr 13, 2020 at 18:58

2 Answers 2

41

Define the migration for the new attribute newAttribute with both a DEFAULT value and as NOT NULL.

Code

database.execSQL("ALTER TABLE tableName ADD COLUMN newAttribute INTEGER DEFAULT 0 NOT NULL")

Full Code

@Database(entities = arrayOf(ModelName::class), version = 2)
@TypeConverters(Converters::class)
abstract class DatabaseName : RoomDatabase() {

    abstract fun daoName(): DaoName

    companion object {

        private var INSTANCE: DatabaseName? = null

        fun getAppDatabase(context: Context): DatabaseName {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.applicationContext,
                        DatabaseName::class.java, DATABASE_NAME)
                        .addMigrations(MIGRATION_1_2)
                        .build()
            }
            return INSTANCE as DatabaseName
        }

        val MIGRATION_1_2: Migration = object : Migration(1, 2) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("ALTER TABLE tableName ADD COLUMN newAttribute INTEGER DEFAULT 0 NOT NULL")
            }
        }
    }

}
2
  • 1
    THANK YOU FOR THE ANSWER :) SAVED ME A LOT OF TIME!
    – Rohit TP
    Commented Oct 8, 2019 at 7:51
  • Can you please help me out to write default value inside entity class for Boolean property. Commented Jan 11, 2023 at 6:26
11

SQLite does not have a boolean data type. Room maps it to an INTEGER column, mapping true to 1 and false to 0. I think below code would be work

database.execSQL("ALTER TABLE xyz_table ADD COLUMN abc INTEGER DEFAULT 0");

It is undocumented.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.