[ALOY-1178] Models: Data not retrieved from/saved to a table altered via migration using a SQL statement
GitHub Issue | n/a |
---|---|
Type | Improvement |
Priority | Medium |
Status | Open |
Resolution | Unresolved |
Affected Version/s | Alloy 1.6.0 |
Fix Version/s | n/a |
Components | Models |
Labels | n/a |
Reporter | Tim Poulsen |
Assignee | Feon Sua Xin Miao |
Created | 2014-10-22T18:42:31.000+0000 |
Updated | 2015-06-08T17:39:22.000+0000 |
Description
Per http://developer.appcelerator.com/question/178611/data-is-not-saved-in-column-after-alter-table-through-alloy-migration
After applying a migration that alters a table structure, data saved to the new columns is not persisted to the SQLite database.
// from the sample app, annotated
function doClick(e) {
var m = Alloy.createModel('books');
m.fetch({id: e.row.rowid}) // rowid is the alloy_id primary key
Ti.API.info(JSON.stringify(m.toJSON()));
// {"title":"Changed","alloy_id":"d6bfe744-8e7a-9b24-c958-1e4275318276","pagecount":null}
m.save({title: 'Changed', pagecount: 200});
Ti.API.info(JSON.stringify(m.toJSON()));
// {"title":"Changed","alloy_id":"d6bfe744-8e7a-9b24-c958-1e4275318276","pagecount":200}
// but looking in the app's db, the pagecount column is still null
Alloy.Collections.books.fetch();
}
Sample / test app attached. Also, screenshot of app's database after migration has been applied and a few new models have been added (any labeled Changed were added after the migration and should have a pagecount)
This behavior is present with Alloy 1.6.0-dev, 1.5.1, 1.4.1, and 1.3.1.
Attachments
File | Date | Size |
---|---|---|
app.zip | 2014-10-22T18:42:31.000+0000 | 7221 |
Screen Shot 2014-10-22 at 2.41.05 PM.png | 2014-10-22T18:42:31.000+0000 | 281563 |
On further investigation, this would never have worked with code as reported. The list of columns is built from the model definition. With a custom SQL command being used to alter the table, there's no way for the sync adapter to know of the existence of that new column. The way to deal with this is to use this technique for adding a new column to the table: First, create your migration file (e.g. with
alloy generate migration MODEL_NAME
) Then, add code like this to that file:Next, you must modify your original model file to also add that same new column. That's what the sync adapter is keyed off of to determine which fields data will be read from / written to. I'm re-casting this ticket as a feature request. Perhaps we could implement a function similar to createTable() (in the migration) that would alter the table to add the field; or, some means in the migration file to update the list of columns; or updated code in the model-generation script that would read the live db table's config.