Ошибка Caused by: java.lang.IllegalStateException: Migration didn't properly handle
Есть два класса Item и Category. Хотел сделать так, чтобы при удалении категории, удалялись все элементы, связанные с ней. Но почему-то при добавлении параметра удаления выходит ошибка.
Caused by: java.lang.IllegalStateException: Migration didn't properly handle
Пробовал и удалить приложение, и пересобрать проект, и на чистое устройство ставил, результатов нет.
Пример обеих классов
Item.class
@Entity(indices = @Index(value = "categoryid"), tableName = "item", foreignKeys = @ForeignKey(entity = Category.class, parentColumns = "id", childColumns = "categoryid", onDelete = CASCADE))
public class Item {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(typeAffinity = INTEGER)
private int id;
@ColumnInfo(typeAffinity = INTEGER)
private long date_no_time;
@ColumnInfo(typeAffinity = INTEGER)
private long date_with_time;
@ColumnInfo(typeAffinity = REAL)
private double summ;
@ColumnInfo(typeAffinity = INTEGER)
private int categoryid;
@ColumnInfo(typeAffinity = INTEGER)
private int valutaid;
@ColumnInfo(typeAffinity = INTEGER)
private int type;
@ColumnInfo(typeAffinity = TEXT)
private String comments;
@ColumnInfo(typeAffinity = TEXT)
private String currency_code;
@Ignore
public Item(int id, long date_no_time, long date_with_time, double summ, int categoryid, int valutaid, int type, String comments, String currency_code) {
this.id = id;
this.date_no_time = date_no_time;
this.date_with_time = date_with_time;
this.summ = summ;
this.categoryid = categoryid;
this.valutaid = valutaid;
this.comments = comments;
this.type = type;
this.currency_code = currency_code;
}
public Item(long date_no_time, long date_with_time, double summ, int categoryid, int valutaid, int type, String comments, String currency_code) {
this.date_no_time = date_no_time;
this.date_with_time = date_with_time;
this.summ = summ;
this.categoryid = categoryid;
this.valutaid = valutaid;
this.comments = comments;
this.type = type;
this.currency_code = currency_code;
}
public String getCurrency_code() {
return currency_code;
}
public void setCurrency_code(String currency_code) {
this.currency_code = currency_code;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public long getDate_no_time() {
return date_no_time;
}
public void setDate_no_time(long date_no_time) {
this.date_no_time = date_no_time;
}
public long getDate_with_time() {
return date_with_time;
}
public void setDate_with_time(long date_with_time) {
this.date_with_time = date_with_time;
}
public double getSumm() {
return summ;
}
public void setSumm(double summ) {
this.summ = summ;
}
public int getCategoryid() {
return categoryid;
}
public void setCategoryid(int categoryid) {
this.categoryid = categoryid;
}
public int getValutaid() {
return valutaid;
}
public void setValutaid(int valutaid) {
this.valutaid = valutaid;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
Item item = (Item)obj;
return item.getDate_no_time() == this.getDate_no_time()
&& item.getDate_with_time() == this.getDate_with_time()
&& item.getSumm() == this.getSumm()
&& item.getCategoryid() == this.getCategoryid()
&& item.getValutaid() == this.getValutaid()
&& item.getType() == this.getType()
&& item.getComments().equals(this.getComments())
&& item.getCurrency_code().equals(this.getCurrency_code());
}
public static DiffUtil.ItemCallback<Item> DIFF_CALLBACK =
new DiffUtil.ItemCallback<Item>() {
@Override
public boolean areItemsTheSame(@NonNull Item oldItem,
@NonNull Item newItem) {
return oldItem.getId() == newItem.getId();
}
@Override
public boolean areContentsTheSame(@NonNull Item oldItem,
@NonNull Item newItem) {
return oldItem.equals(newItem);
}
};
}
Category.class
@Entity (indices = {@Index("id")},tableName = "category")
public class Category {
@ColumnInfo(typeAffinity = INTEGER)
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(typeAffinity = TEXT)
private String text;
@ColumnInfo(typeAffinity = TEXT)
private String comments;
@ColumnInfo(typeAffinity = TEXT)
private String icon;
@ColumnInfo(typeAffinity = TEXT)
private String image;
@ColumnInfo(typeAffinity = INTEGER)
private int type;
@Ignore
public Category(int id, String text, String comments, String icon, String image, int type) {
this.id = id;
this.text = text;
this.comments = comments;
this.icon = icon;
this.image = image;
this.type = type;
}
public Category(String text, String comments, String icon, String image, int type) {
this.text = text;
this.comments = comments;
this.icon = icon;
this.image = image;
this.type = type;
}
public int getId() {
return id;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public void setId(int id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
Category item = (Category)obj;
return item.getText().equals(this.getText());
}
public static DiffUtil.ItemCallback<Category> DIFF_CALLBACK =
new DiffUtil.ItemCallback<Category>() {
@Override
public boolean areItemsTheSame(@NonNull Category oldItem,
@NonNull Category newItem) {
return oldItem.getId() == newItem.getId();
}
@Override
public boolean areContentsTheSame(@NonNull Category oldItem,
@NonNull Category newItem) {
return oldItem.equals(newItem);
}
};
}
Ошибка:
java.lang.RuntimeException: Unable to resume activity {test.myapp/test.myapp.activity.MainActivity}: java.lang.IllegalStateException: Migration didn't properly handle item(test.myapp.models.Item).
Expected:
TableInfo{name='item', columns={date_no_time=Column{name='date_no_time', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, comments=Column{name='comments', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, summ=Column{name='summ', type='REAL', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}, valutaid=Column{name='valutaid', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, date_with_time=Column{name='date_with_time', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, type=Column{name='type', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, categoryid=Column{name='categoryid', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, currency_code=Column{name='currency_code', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[ForeignKey{referenceTable='category', onDelete='CASCADE', onUpdate='NO ACTION', columnNames=[categoryid], referenceColumnNames=[id]}], indices=[Index{name='index_item_categoryid', unique=false, columns=[categoryid]}]}
Found:
TableInfo{name='item', columns={date_no_time=Column{name='date_no_time', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, comments=Column{name='comments', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, summ=Column{name='summ', type='REAL', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}, valutaid=Column{name='valutaid', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, date_with_time=Column{name='date_with_time', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, type=Column{name='type', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, categoryid=Column{name='categoryid', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, currency_code=Column{name='currency_code', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[Index{name='index_item_categoryid', unique=false, columns=[categoryid]}]}
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3784) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3816) at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51) at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Если в аннотации обеих классов удалить все индексы и внешние связи и оставить только @Entity(tableName = "item") и @Entity(tableName = "category"), то приложение запускается.