Pavel Petrashov
Pavel Petrashov

Reputation: 139

How to update a table in ORM "ActiveAndroid"?

I use in my project "ActiveAndroid". I have a model which I retain. here is a model:

@Table(name="Params")
public class Params extends Model {

    public Params() {
        super();
    }

    @Expose
    @Column(name="height")
    @SerializedName("heigth")
    public int heigth;

    @Expose
    @Column(name="weight")
    @SerializedName("weight")
    public int weight;
}

here all save:

public void success(User user, Response response) {
               user.params.save();
               user.save();
                ActiveAndroid.clearCache();
            }

Everything works fine! But if I want to add another field in the model:

@Expose
    @Column(name="strong")
    @SerializedName("strong")
    public int strong;

I get an error:

android.database.sqlite.SQLiteException: table Params has no column named strong (code 1): , while compiling: INSERT INTO Params(Id,weight,height,strong) VALUES (?,?,?,?)

of course I know why this error. Because the table is no such column. That's the question, how to add this column ???

Now that I've tried:

  1. remove programs completely, again to compile, run, and everything works fine! because it creates a new table from this column.

  2. Tried to change the version in the manifest database, but this has not led to success.

I know that with normal use of the database in the method of upgrade you can change the version of the database and restructure table. but how to do it in ORM "ActiveAndroid" ?

Upvotes: 0

Views: 1118

Answers (1)

Murtaza Khursheed Hussain
Murtaza Khursheed Hussain

Reputation: 15336

There is a thing known as Migration, in Active Android you can do that like this

  1. Add the field in your model (which you already did)

  2. Change the database version the the AndroidManifest.xml's metadata

  3. Write your migration script. Name your script [newDatabaseVersion].sql, and place it in the directory [YourApp’sName]/app/src/main/assets/migrations. In my specific example, I’ll create the file [MyAppName]/app/src/main/assets/migrations/2.sql.

E.G. ALTER TABLE Items ADD COLUMN Priority TEXT;

Upvotes: 4

Related Questions