A.Danibo
A.Danibo

Reputation: 624

Android Room change type of column with Migration

I didn't find any answer to what I'm searching for so I'm sending this question.

I am using Room for Android.

I have an Entity with an Int column and I need to change it to Double, and I don't know how to do it.

Does anybody know how to do it?

My question might be dumb, but I didn't find any answer on stackoverflow/any google search.

Upvotes: 11

Views: 14880

Answers (2)

Sebastian M
Sebastian M

Reputation: 659

You need to create new table with the new schema. Copy data from old table to the new one. Drop old table. Rename new table to the name of old table.

Here you've got nice article about that: https://medium.com/androiddevelopers/understanding-migrations-with-room-f01e04b07929

Upvotes: 17

karan
karan

Reputation: 8853

As mentioned in documentation for sqlite over here, updating column type is not supported in sqlite.

SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table. It is not possible to rename a colum, remove a column, or add or remove constraints from a table.

As pointed by Sebastian M you will need to create new table with the new column added, Copy data from old table to the new one. Drop old table. Rename new table to the name of old table.

Upvotes: 6

Related Questions