Raza
Raza

Reputation: 809

Room database migration issue with column type Boolean

I have implemented the Room database in my Application with few tables. Now I have to add another table in my database as the requirement has been updated. The problem I am facing is that when I execute the code with column type boolean App crashes. My entity class model is as below

@Entity(tableName = Constants.TABLE_NAME_CONVERSATION)
public class ConversationModel {

@PrimaryKey(autoGenerate = true)
private int id;

String inputWord, translatedWord,origin,targetLangCode;
boolean isSpeaking;


public ConversationModel() {
}
}

The migration code I have written is :

 private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {

        String createTable = "CREATE TABLE IF NOT EXISTS 'conversation' (id  INTEGER NOT NULL PRIMARY KEY, inputWord TEXT, translatedWord TEXT, origin TEXT, targetLangCode TEXT, isSpeaking BOOLEAN)";

        database.execSQL(createTable);
    }
};

And the crash log I am receiving is this:

 Caused by: java.lang.IllegalStateException: Migration didn't properly handle conversation(com.translateall.language.free.translator.dictionary.speechtext.learnenglish.models.ConversationModel).
 Expected:
TableInfo{name='conversation', columns={isSpeaking=Column{name='isSpeaking',type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
 Found:
 2019-10-29 13:24:31.433 20525-20525/com.translateall.language.free.translator.dictionary.speechtext.learnenglish E/AndroidRuntime:TableInfo{name='conversation', columns={
  isSpeaking=Column{name='isSpeaking', type='BOOLEAN', affinity='1', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}

My model class has boolean type item. Why it is expecting integer type?

Upvotes: 2

Views: 1365

Answers (1)

MikeT
MikeT

Reputation: 57043

ROOM only allows column types of TEXT, INTEGER, REAL or BLOB.

You need to use isSpeaking INTEGER. This will work with boolean isSpeaking;

So you should use :-

String createTable = "CREATE TABLE IF NOT EXISTS 'conversation' (id  INTEGER NOT NULL PRIMARY KEY, inputWord TEXT, translatedWord TEXT, origin TEXT, targetLangCode TEXT, isSpeaking INTEGER)";

P.S. a means of getting the correct SQL is to compile, after changing the entity, and to then look at the generated java of the ??????_impl java for the database class (where ???? is the class) the create SQL will be in the createAllTables method.

Upvotes: 2

Related Questions