Shura Capricorn :
I have created a director
class
@Entity(tableName = "director")
public class Director {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "did")
public int id;
@ColumnInfo(name = "full_name")
@NonNull
public String fullName;
public Director(@NonNull String fullName) {
this.fullName = fullName;
}
}
I decided to add a new column by adding a migration method and adding a field to the class
@Entity(tableName = "director")
public class Director {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "did")
public int id;
@ColumnInfo(name = "full_name")
@NonNull
public String fullName;
@ColumnInfo(name = "age")
public int age;
public Director(@NonNull String fullName) {
this.fullName = fullName;
}
}
the method
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
String query = "ALTER TABLE 'director' ADD COLUMN 'age' INTEGER ";
database.execSQL(query);
}
};
When I execute the code I get this error
Caused by: java.lang.IllegalStateException: Migration didn't properly handle
And it says it expected
age=Column{name='year', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}},
And found
age=Column{name='year', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'},
I can see the problem is on the "notnull" part but how to fix this? I want it to be null.
Amos Korir :
Try setting the default age value to 0
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL(
"ALTER TABLE 'director' ADD COLUMN 'age' INTEGER NOT NULL DEFAULT 0"
);
}
};
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=334250&siteId=1