pardom-zz / ActiveAndroid

Active record style SQLite persistence for Android
http://www.activeandroid.com
4.7k stars 1.03k forks source link

Pre populate table using schema migration #308

Open ido567 opened 9 years ago

ido567 commented 9 years ago

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 the library, 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{

@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');

lhoracek commented 9 years ago

I think you have the id column name wrong. Should be "Id" as created by default by ActiveAndroid. Or use the prepopulated DB feature (place existing db in asset directory)

ido567 commented 9 years ago

Even after changing the Id field, there is no change. As for now, I want to avoid using prepopulated DB. There is no other way to achieve that?

lhoracek commented 9 years ago

Where is the 1.sql located? Is it assets/migrations? Try putting breakpoint to DatabaseHelper.executeMigrations() if it finds your upgrade script and executes it.

On Fri, Nov 28, 2014 at 6:20 PM, Ido Bublil notifications@github.com wrote:

Even after changing the Id field, there is no change. As for now, I want to avoid using prepopulated DB. There is no other way to achieve that?

— Reply to this email directly or view it on GitHub https://github.com/pardom/ActiveAndroid/issues/308#issuecomment-64915057 .

S pozdravem Luboš Horáček

ido567 commented 9 years ago

The script is skipped because the file number (1) is bigger than the new version number (0). I think there is a bug in the new version number.. Anyway, changing the file name to 0.sql made the trick for me

lhoracek commented 9 years ago

Check your manifest file for defined database version in meta tag AA_DB_VERSION.

On Mon, Dec 1, 2014 at 6:16 AM, Ido Bublil notifications@github.com wrote:

The script is skipped because the file number (1) is bigger than the new version number (0). I think there is a bug in the new version number.. Anyway, changing the file name to 0.sql made the trick for me

— Reply to this email directly or view it on GitHub https://github.com/pardom/ActiveAndroid/issues/308#issuecomment-65021772 .

S pozdravem Luboš Horáček

ido567 commented 9 years ago

AA_DB_VERSION = 1

lhoracek commented 9 years ago

How come it gives you version number 0 then. Is your source available somewhere?

On Mon, Dec 1, 2014 at 3:52 PM, Ido Bublil notifications@github.com wrote:

AA_DB_VERSION = 1

— Reply to this email directly or view it on GitHub https://github.com/pardom/ActiveAndroid/issues/308#issuecomment-65074988 .

S pozdravem Luboš Horáček

ido567 commented 9 years ago

I can share my bitbucket project with you, I just need your username

lhoracek commented 9 years ago

Same as here "lhoracek"

ido567 commented 9 years ago

Done, check it out

lhoracek commented 9 years ago

Your code works for me. Are you sure you uninstalled you application before trying that script so old version of DB is set to -1 on runtime?

ido567 commented 9 years ago

Yes, the old version was -1 but the new version was 0!!

lhoracek commented 9 years ago

Thats how it's working for me. Inserts are performed as expected and row are in DB. Try uninstalling application completely first and then installing again to have the database recreated.

On Thu, Dec 4, 2014 at 3:36 PM, Ido Bublil notifications@github.com wrote:

Yes, the old version was -1 but the new version was 0!!

— Reply to this email directly or view it on GitHub https://github.com/pardom/ActiveAndroid/issues/308#issuecomment-65640075 .

S pozdravem Luboš Horáček

ido567 commented 9 years ago

Using file name of 0.sql is working for me too, but my problem is when I'm using the "1.sql" name. Did you manage to make it work with 0.sql or 1.sql?