rkdroid
rkdroid

Reputation: 149

Room Database Migration doesn't properly handle New table creation

The log contains :

java.lang.IllegalStateException: Migration didn't properly handle broadBandPlans(duleaf.duapp.splash.data.local.models.FixedPlanLocal).

*Expected:
TableInfo{name='broadBandPlans', columns={speed=Column{name='speed', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, prodID=Column{name='prodID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1}}, foreignKeys=[], indices=[]}*

*Found:
TableInfo{name='broadBandPlans', columns={prodID=Column{name='prodID', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1}, speed=Column{name='speed', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}*

Below is the FixedPlanLocal Class, Can someone guide me here why Room is expecting speed as the first column and then prodID, how should I solve this?

@Entity(tableName = "broadBandPlans")
public class FixedPlanLocal {

    @ColumnInfo(name = "prodID")
    @PrimaryKey
    @NonNull
    String prodID;

    @ColumnInfo(name = "speed")
    String speed;


    public FixedPlanLocal(@NonNull String prodID, String speed) {
        this.prodID = prodID;
        this.speed = speed;
    }

    @NonNull
    public String getProdID() {
        return prodID;
    }

    public void setProdID(@NonNull String prodID) {
        this.prodID = prodID;
    }

    public String getSpeed() {
        return speed;
    }

    public void setSpeed(String speed) {
        this.speed = speed;
    }

    @Override
    public boolean equals(Object obj) {
        return this.prodID.equals(((FixedPlanLocal) obj).prodID);
    }
}
, 

Upvotes: 0

Views: 78

Answers (1)

MikeT
MikeT

Reputation: 56928

You've defined the speed column, of the table, using the NOT NULL constraint, therefore you need @NonNull in the entity for the speed column:-

  • Expected is as per the Entity
  • Found is as per the Table

Instead of :-

public String getSpeed() {
    return speed;
}

Use :-

@NonNull
public String getSpeed() {
    return speed;
}

The column order is irrelevant and Room seems to quite persistently not order them as expected (i.e. not as per either the Table or Entity for the expected list).

What is important are the values must be consistent between expected and found.

In your case The Entity (expected) and the Table (Found) for the speed column are :-

name='speed', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0
name='speed', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0

You can easily see that notNull is false for the Entity (@NotNull is not coded) but true for the Table.

The fix is to either add @NotNull to the Entity (as above) or the change the Table definition so that the speed column does not have the NOT NULL constraint.

  • Note, in some situations e.g. when using java primary types in the Entity, that @NotNull is implied (in such situations you have to change the table (or use an equivalent object instead of the java primary type e.g. use Integer instead of int))

Upvotes: 1

Related Questions