Search code examples
androidandroid-sqliteactiveandroid

ActiveAndroid Pre populate table using schema migration


I want to create a table for the first time (DB version = 1), and insert into it 2 rows by default. The table needs to be created automatically by ActiveAndroid, and the records should be inserted by the SQL I wrote in 1.sql file.

The table looks fine, but the rows are not inserted at all (no errors thrown).

The model looks like this:

@Table(name = "leagues")
public class League extends Model implements Serializable{

    @Column(name = "name")
    public String name;

    public static List<League> getAll() {
        return new Select()
                .from(League.class)
                .orderBy("name ASC")
                .execute();
    }
}

and the 1.sql:

INSERT INTO leagues (Id, name) VALUES (1, 'Premier league');

Solution

  • Apparently that ActiveAndroid thinks the version of the DB is 0 in the initial setup of the app. So, the file "1.sql" has a bigger version than the DB number and it will be skipped!

    To make it work, the script name should be "0.sql".