I have a Room SQL table with several columns. And I want to add one more column to it containing a boolean value. For migrating my database to this new schema, I am trying to use Room Auto-Migrations . My question is how can I update my existing rows in the table to have a value false
for this newly added column? Will I have to fallback to writing manual migration by altering the table and inserting the default value to all rows?
My question is how can I update my existing rows in the table to have a value false for this newly added column?
Use the @ColumnInfo
's defalutValue eg for the column in the Entity.
@ColumnInfo(defaultValue = "0")
Will I have to fallback to writing manual migration by altering the table and inserting the default value to all rows?
This should then add column with the default value clause when Auto Migrating.
Additional
If you then wanted to Insert using the default value you would not be able to use the convenience @Insert
annotation in the respective interface/asbstract class that is annotated with @Dao but would have to use an @Query
that list the columns omitting the columns that are to use the default, and that has the VALUES clause specifying the values of the named columns.
eg for a table with 3 columns, columna, columnb and columnc; where columnc has a DEFAULT specified then the insert function could be something like:-
@Query("INSERT INTO the_table (columna,columnb) VALUES(:valueForColumnA,:valueForColumnB);")
fun insertWithDefaultValueForColumnC(valueForColumnA: TheType,valueForColumnB: TheType): Long
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.