dimanche 7 février 2016

Can't access data after SugarORM Migration

I am having issues with database migration using SugarORM (1.4) in my Android project.

I want to add 2 columns in an existing table (NOTE).

In the migration SQL script (assets/sugar_upgrades/2.sql), I have:

ALTER TABLE NOTE ADD COLUMN created_by INTEGER REFERENCES USER(id);
ALTER TABLE NOTE ADD COLUMN is_synced INTEGER DEFAULT 0;

I also added 2 integers in Note.class, which extends SugarRecord (createdBy, isSynced).

This adds 2 columns to the table. The problem is when I try to retrieve data from NOTE table.

List<Note> note = Note.listAll(Note.class); : returns an empty list.
List<Note> allNotes = Lists.newArrayList(Note.findAll(Note.class)); : returns the records, but if I write:

allNotes.get(0).setTitle('title');
allNotes.get(0).setBody('body text');
allNotes.get(0).save();

it inserts a new row in the table instead of updating the first row. Further looking into it, I found that: allNotes.get(0).getId(); // returns null.

Putting a blank upgrade SQL file does not add columns to the table, even if the new integers/columns are defined in Note.class.

In case of creating new tables the migration works fine.

Please let me know what the issue is. If it is a bug please look into it.

Thanking in advance.

Aucun commentaire:

Enregistrer un commentaire