'Room database - edit entities
I have a question. I edited my Room entity from this:
@Entity(tableName = "users")
public class User {
public User(String username, String email, String password){
this.username = username;
this.email = email;
this.password = password;
}
public User() {}
@PrimaryKey
@ColumnInfo(name = "Username")
private String username;
@ColumnInfo(name = "Email")
private String email;
@ColumnInfo(name = "Password")
private String password;
//getters setters
To this form:
@Entity(tableName = "users")
public class User {
public User(String username, String email, String password){
this.username = username;
this.email = email;
this.password = password;
}
public User() {}
//changes here, move userId to int instead of username
@NonNull
@PrimaryKey(autoGenerate = true)
private int id;
@NonNull
@ColumnInfo(name = "Username")
private String username;
@ColumnInfo(name = "Email")
private String email;
@ColumnInfo(name = "Password")
private String password;
//getters setters.
And this is how looks my RoomDatabase
class:
@Database(entities = {User.class}, version = 1)
public abstract class ApplicationDatabase extends RoomDatabase {
public abstract UserDao userDao();
}
My question is when I edit an entity if the database in my android device change? What if I had data in my database? If I need to drop the database on my device first? What about my constructor for user, it will work now?
Thanks for any advice
Solution 1:[1]
When you edit your entities you need to change the version of your database, in that case, change the annotation to @Database(entities = {User.class}, version = 2)
It is up to you to decide whether you want to drop old data of your database or you want to migrate the old version to the newer one (I would recommend it if you have a user base and you don't want them to lose their saved data in the database otherwise I would recommend you to drop the information in the old database because it is the easier solution).
In case you want to read further about migrating your room database I have added a link:
https://developer.android.com/training/data-storage/room/migrating-db-versions
Solution 2:[2]
As mentioned by you, you don't have any issue in deleting and recreating your database. Then in that case, just add fallbackToDestructiveMigration()
to your Room
db builder like:
Room.databaseBuilder(getApplicationContext(), MyDb.class, "database-name")
.fallbackToDestructiveMigration() //recreate DB in case of conflict due to cache/backup
.build();
This will recreate all the tables when you bump your database version, losing all your previous data.
In case you want to update tables while preserving older data in database, you will need to use MIGRATIONS like:
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("CREATE TABLE `Fruit` (`id` INTEGER, "
+ "`name` TEXT, PRIMARY KEY(`id`))");
}
};
Room.databaseBuilder(getApplicationContext(), MyDb.class, "database-name")
.addMigrations(MIGRATION_1_2).build();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gal |
Solution 2 | ankuranurag2 |