GitHub Issue | n/a |
Type | Bug |
Priority | High |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2013-03-15T22:05:15.000+0000 |
Affected Version/s | Alloy 1.0.0 |
Fix Version/s | Alloy 1.1.0, 2013 Sprint 06 |
Components | Titanium SDK |
Labels | alloy, ios, model, sqlite |
Reporter | David Welch |
Assignee | Tony Lukasavage |
Created | 2013-03-05T21:00:50.000+0000 |
Updated | 2013-03-26T01:07:53.000+0000 |
The example of idAttribute in the alloy test examples (sql_keywords) works, but when setting idAttribute to a model attribute that is of type text it won't persist the data.
Example app attached.
Did you try this on a fresh project, or did you change the idAttribute after the SQLite database had already been built?
I tried this on a fresh project. After it didn't work I deleted the app and tried it without idAttribute where it worked.
It should be noted that it only works when artist_id is "integer PRIMARY KEY AUTOINCREMENT". If it's "integer PRIMARY KEY" or "text" the data won't persist. I delete the database each time to start fresh.
Clearly, when I set artist_id to "integer PRIMARY KEY AUTOINCREMENT" or "integer PRIMARY KEY" I change my code in index.js to set it to an integer instead of string.
That's weird since the default behavior in the absence of the idAttribute is for alloy to create one itself, a GUID that is a TEXT field named "alloy_id". I'll have to dig into your sample to see why it's not working in your case.
I thought so as well since alloy_id is text. Either way I wasn't able to persist the data.
Ran into this bug too. // idAttribute = "email" // This silently fails to save var userM = Alloy.createModel('userModel', {email: userEmail, name: "Test"}); userM.save(); // This works var userM = Alloy.createModel('userModel', {email: userEmail, name: "Test"}); userM.id = null; userM.save(); Looks like the root cause of this bug is that when idAttribute value is set the model.id is also assigned immediately, before its saved. Too soon. That causes backbone's isNew() to return false at save, and attempt an update instead of a create. Which then silently fails obviously (it'll even call the success function if you set one!) A hack-around is to set model.id = null after createModel() but before save().
I found the same Kimberlee, apologies for not logging it here earlier to save the time of others investigating. I'm working on this one now.
This turned out to be a lot more complicated than expected initially. Mainly because the resolution to this issue involved changing the way the sql adapter inserts and updates, which is now based on DB constraints. This in turn changed the properties of the default alloy_id field used for automatically assigning unique ids to models, which had implications on adding alloy_id to existing databases, particularly preloaded ones. Details of all this are covered in this commit (props to Aaron Saunders for the initial code for these changes): https://github.com/appcelerator/alloy/commit/d511ebd64b5b1e8f840d1be80b90f0639d251e87 In addition, the core issue of this ticket can be seen addressed in the following test app: https://github.com/appcelerator/alloy/tree/master/test/apps/models/sql_idAttribute