code4rox
code4rox

Reputation: 1093

Migration didn't properly handle Room database (java.lang.IllegalStateException)

I have Fatal Exception: java.lang.RuntimeException: Exception while computing database live data. in crash report

Why here every thing is empty in TableInfo

Expected:

TableInfo{name='card_data', columns={bPathImg=Column{name='bPathImg', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, cardName=Column{name='cardName', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, cardId=Column{name='cardId', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, cardType=Column{name='cardType', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, time=Column{name='time', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, fPathImg=Column{name='fPathImg', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, sqlDate=Column{name='sqlDate', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, cardNumber=Column{name='cardNumber', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}

Found:

TableInfo{name='card_data', columns={}, foreignKeys=[], indices=[]}

Here is my entity class

@Entity(tableName = "card_data")
@Keep
public class CardDataEntity {

    @PrimaryKey(autoGenerate = true)
    private int cardId;
    private String cardName;
    private String time;
    private String sqlDate;
    private int cardNumber;
    private String fPathImg;
    private String bPathImg;
    private String cardType;
    @Ignore
    private boolean isHeader;

    public CardDataEntity(String cardName, String time, String sqlDate, int cardNumber, String fPathImg, String bPathImg, String cardType) {

        this.cardName = cardName;
        this.time = time;
        this.sqlDate = sqlDate;
        this.cardNumber = cardNumber;
        this.fPathImg = fPathImg;
        this.bPathImg = bPathImg;
        this.cardType = cardType;
        this.isHeader = false;

    }

    // setter getter here

}

and here is my database class

@Database(entities = {ScanDataEntity.class , ScanDataBookmarkEntity.class,
        GenerateDataEntity.class, GenerateBookmarkDataEntity.class, CardDataEntity.class, CardBookmarkDataEntity.class},
        version = 7, exportSchema = false)

public abstract class ScanDatabase extends RoomDatabase {
    public abstract ScanDataDao scanDataDao();
    // DAO classes

    private static ScanDatabase dataBase;

    public static ScanDatabase getInstance(Context context){
        if (null== dataBase){
            dataBase= buildDatabaseInstance(context);
        }
        return dataBase;
    }

    private static final Migration MIGRATION_6_7 = new Migration(6, 7) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            try {
                database.execSQL("ALTER TABLE generate_data "
                        + " ADD COLUMN generateImgPath TEXT");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    };
   
    // same migration for other versions 

    private static ScanDatabase buildDatabaseInstance(Context context) {
        return Room.databaseBuilder(context,
                ScanDatabase.class,
                "scan_database")
                .addMigrations(MIGRATION_6_7,MIGRATION_5_7,MIGRATION_4_7,MIGRATION_3_7,MIGRATION_2_7,MIGRATION_1_7)
//                .fallbackToDestructiveMigration()
                .allowMainThreadQueries().build();

    }

}

Can anyone help me please, I really don't know what I'm doing wrong or if it is a bug.

Upvotes: 1

Views: 2521

Answers (2)

hb0
hb0

Reputation: 3747

Ended up here with the same error. For future readers who end up here, too:

In my case I had a simple typo in the self-generated Schema-files:

Schema 8.json

"tableName": "measurement",
        "createSql": "CREATE TABLE measurement...

Migration 8 to 12:

database.execSQL("ALTER TABLE measurement RENAME TO measurements;")

Schema 12.json:

"tableName": "measurement",
        "createSql": "CREATE TABLE measurements ...
  • The cause for the error was: tableName should be measurmentS not measurement

Hope this helps others, too, as the error itself did not really help me identifying this typo.

Upvotes: 0

Stanislav Bondar
Stanislav Bondar

Reputation: 6265

Specify column names with @ColumnInfo annotation to provide column data associated with this field. Like

@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "cardId")
private int cardId;
.....

Upvotes: 0

Related Questions