Search code examples
androidandroid-sqliteandroid-room

Create Room Entity for a Table which has a field with LONG datatype in Sqlite


App Database has Items table with a column Price with datatype Long. Db Version = 1

CREATE TABLE items (_id INTEGER PRIMARY KEY AUTOINCREMENT,item_id 
INTEGER,title TEXT,price LONG, UNIQUE (item_id) ON CONFLICT IGNORE)

While trying to migrate to Room I am experiencing below issue

java.lang.IllegalStateException: Migration didn't properly handle items(moka.pos.test.data.entity.Item).

Expected : price=Column{name='price', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}
Found : price=Column{name='price', type='LONG', affinity='1', notNull=false, primaryKeyPosition=0}

Here is my entity class for Item

@Entity(tableName = "items")
public class Item {

@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "_id")
private Integer _ID;

@ColumnInfo(name = "item_id")
private Integer id;

@ColumnInfo(name = "title")
private String title;

@ColumnInfo(name = "price")
private Long price;

public Integer get_ID() {
    return _ID;
}

public void set_ID(Integer _ID) {
    this._ID = _ID;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public Long getPrice() {
    return price;
}

public void setPrice(Long price) {
    this.price = (long) (getId() * AppUtil.getRandomNumber(10, 99));
}
}

How to make the Room entity field to support Long datatype when migration from SQLiteOpenHelper to Room.


Solution

  • The simple answer is you CANNOT

    Room only supports 5 data types which are TEXT, INTEGER, BLOB, REAL and UNDEFINED.

    So, java data types of Boolean, Integer, Long will be all converted to INTEGER in SQL.

    What you can do is convert your LONG data type to INTEGER in SQL instead of converting INTEGER data type to LONG in Room in order to make Room support LONG, which Room doesn't support.