1

I have written Android Room migrations, but somehow it does not handle it properly.

Here is the error:

    Expected:
TableInfo{name='Likes', columns={creatorId=Column{name='creatorId', type='TEXT', notNull=false, primaryKeyPosition=0}, messageId=Column{name='messageId', type='TEXT', notNull=false, primaryKeyPosition=0}, createdAt=Column{name='createdAt', type='TEXT', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='TEXT', notNull=false, primaryKeyPosition=0}, dbId=Column{name='dbId', type='INTEGER', notNull=true, primaryKeyPosition=1}, timestamp=Column{name='timestamp', type='TEXT', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
 Found:
TableInfo{name='Likes', columns={dbId=Column{name='dbId', type='INTEGER', notNull=true, primaryKeyPosition=1}, creatorId=Column{name='creatorId', type='TEXT', notNull=false, primaryKeyPosition=0}, messageId=Column{name='messageId', type='TEXT', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='TEXT', notNull=false, primaryKeyPosition=0}, timestamp=Column{name='timestamp', type='TEXT', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}

Here is migration script:

database.execSQL("CREATE TABLE Likes (id TEXT, creatorId TEXT, messageId TEXT,"
                       + " timestamp TEXT, dbId INTEGER NOT NULL, PRIMARY KEY(dbId))");

Here is model class:

@Entity(tableName = "Likes")
public class LikeDbModel {

@PrimaryKey private int dbId;
private String id;
private String creatorId;
private String messageId;
private String timestamp;
private String createdAt;

public LikeDbModel() {
}
}

Can anyone help?

Zookey
  • 2,637
  • 13
  • 46
  • 80

2 Answers2

1

It seems you are trying to add the column createdAt. When you create your database add the line .addMigrations(MIGRATION_1_2) to the creation of the database, which I think you have done.

INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                        YourDatabase.class, "your_database.db")
                        .addMigrations(
                                MIGRATION_1_2
                        )
                        .build();

Then implement the migration properly by first dropping the table and then creating it or using an alter table statement.

    static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(SupportSQLiteDatabase db) {
           //Either
           db.execSQL("drop table Likes"); //if existing table
           db.execSQL("CREATE TABLE Likes (id TEXT, creatorId TEXT, messageId TEXT,"
                   + " timestamp TEXT, createdAt TEXT, dbId INTEGER NOT NULL, PRIMARY KEY(dbId))")

            //or
            db.execSQL("alter table add createdAt TEXT");

    }
};

The error you are getting is because room is comparing your entity to the existing table in the database. You could also wrap the migration statements in a try/catch SQLiteException to see what is working.

If you don't want to lose the data in your table, use an alter table instead. If you want to drop/create, the make sure you copy the data over to a new table first with the new structure, copy the data to the new table, drop your original Likes table and rename you new table to Likes. See example here

Room documentation: https://developer.android.com/training/data-storage/room/migrating-db-versions

SammyT
  • 759
  • 1
  • 9
  • 19
-1

Its missing the createdAt field in the migration

bradm6s
  • 102
  • 5