Search code examples
backbone.jstitaniumtitanium-mobiletitanium-alloy

Titanium Alloy Models - Table has no Column


I'm new to Titanium and Alloy but I feel comfortable with most of it at after about 3 months of use. I am however pretty new to Models in Alloy and I'm running into an error which I can't figure out. The following is how my model and controller is set up (excluding the view .XML file as it's irrelevant)

Orders.js | Model

exports.definition = {
config : {
    columns : {
        "id_Orders" : "INTEGER PRIMARY KEY AUTOINCREMENT",
        "reseller_name" : "TEXT",
        "reseller_invoice_account" : "TEXT"
    },
    adapter : {
        type : "sql",
        collection_name : "orders",
        idAttribute: "id_Orders"
    }
}

Orders.js | Controller

var network = require("network");
network.soapRequest("<prem:getResellersRequest/>", function(a) {
    Ti.API.info("The Soap Server Returned: " + JSON.stringify(a));
    Ti.API.info("There are " + a.reseller_name.length + " returned records.");
    for (var i = 0; a.reseller_name.length > i; i++) {
        var listModel = Alloy.createModel("orders", {
            reseller_name: a.reseller_name[i],
            reseller_invoice_account: a.reseller_invoice_account[i]
        });
        listModel.save();
        Alloy.Collections.orders.fetch();
    }
});
Alloy.Collections.orders.fetch();

The error occurs at the listModel.save() line:

Error executing sql: table orders has no column named reseller_name: , while compiling: REPLACE INTO orders (id_Orders,reseller_name,reseller_invoice_account) VALUES (?,?,?);

Network is a lib that sends a SOAP request to a webserver and converts it to JSON using soap2json.js. I'm convinced there's nothing wrong with the network part. I'm pretty sure there's noting wrong with the returned JSON object 'a'. I can output all of the values individually and use it elsewhere - the error occurs when I'm trying to save the model to the collection.

Am I missing something?


Solution

  • That usually happens when you modify the definition of the model after a first run of the application, probably you and added the reseller_name column after that. You must regenerate the table.

    When it happens to me and I'm in the development stage, I usually do a DROP of the table at the beginning of alloy.js file. Try this:

       var db = Ti.Database.open ('_alloy_');
       db.Execute ('DROP TABLE IF EXISTS orders;');
       db.close ();
    

    Obviously this destroys any data in the table, but Alloy regenerate the table with the new definition the first time you instancies a collection or a model.

    The proper way to handle these changes if you need to keep your data is to use migrations