3

In my app I'm using `ActiveAndroid'.
During the app version I'm using migration files and increasing the db version as in documentation.
Assume:
1. There is a device with app version 1 (user doesn't update).
2. In app version 2 I add a new table "myNewTable".
3. In app version 3 I add a new column "myNewColumn" to "myNewTable".
4. In app version 4 the is now change in the db.

migration script:

alter table myNewTable add column myNewColumn text;

The user with app version 1 upgrade to version 4 and getting the following exception:

android.database.sqlite.SQLiteException: duplicate column name: myNewColumn (code 1): , while compiling: alter table myNewTable add column myNewColumn text

Similar to the that problem.

Does someone know to resolve that issue?

NickF
  • 5,637
  • 12
  • 44
  • 75
  • Please post your migration script? – PEHLAJ Mar 28 '17 at 12:38
  • @Pehlaj, I updated the migration script in the body. But it 100% works – NickF Mar 28 '17 at 12:41
  • Make sure migrations script name is same as that AA_DB_VERSION in manifest file. – PEHLAJ Mar 28 '17 at 12:42
  • If AA_DB_VERSION is 4 then, script name should be 4.sql placed under assets/migrations/4.sql – PEHLAJ Mar 28 '17 at 12:43
  • http://sqlite.1065341.n5.nabble.com/ALTER-TABLE-ADD-COLUMN-IF-NOT-EXISTS-td79667.html – PEHLAJ Mar 28 '17 at 12:59
  • https://forums.codewalkers.com/database-help-83/alter-table-structure-conditional-adding-columns-if-they-dont-already-856291.html – PEHLAJ Mar 28 '17 at 13:03
  • @Pehlaj as I said before, all migration files and the db version is ok. The problem that the old version doen't have that table and sinc then that table has been altered. ActiveAndroid creates the whole table and tries to run the script on already existing table that has that column. – NickF Mar 28 '17 at 13:03
  • Please see the 2nd last answer which explains how to add column conditionally – PEHLAJ Mar 28 '17 at 13:07
  • https://forums.codewalkers.com/database-help-83/alter-table-structure-conditional-adding-columns-if-they-dont-already-856291.html – PEHLAJ Mar 28 '17 at 13:07
  • The approach that I use circumvents migration (or minimise them) and the use of version numbers. In short I use a pseudo schema that is compared with the existing database. Tables and columns are then added if they exist in the pseudo schema but not in the database. To add a table or column I just amend the code that builds the pseudo schema. Not a quick fix by any means though. – MikeT Mar 28 '17 at 20:20

0 Answers0