执行id fetch时忽略idAttribute设置

时间:2014-10-21 16:48:37

标签: backbone.js titanium-alloy

我定义了一个模型,它指定了Events.js模型文件中的标识列:

exports.definition = {
    config: {
        columns: {
            "eventId": "string PRIMARY KEY",
            "eventDate": "string",
            "eventName": "string",
            "street1": "string",
            "city": "string",
            "state": "string",
            "zip": "string"
        },
        adapter: {
            type: "sql",
            collection_name: "Events",
            idAttribute:"eventId",
            db_name:"_Fundraising"
        }
    },
    extendModel: function(Model) {
        _.extend(Model.prototype, {
            // extended functions and properties go here
        });

        return Model;
    },
    extendCollection: function(Collection) {
        _.extend(Collection.prototype, {
            // extended functions and properties go here
        });

        return Collection;
    }
};

它正确地创建了数据库文件,但是当我尝试使用特定ID从数据库中检索时,创建的SQL使用默认的alloy_id标识符,而不是配置中指定的eventId

var events = Alloy.createCollection('Events');
events.fetch({ id : "62243" });

完整的错误消息:

2014-10-21 12:22:03.223 Fundraising[53498:946130] [ERROR] A SQLite database error occurred on database 
*** file path removed ***
: Error Domain=com.plausiblelabs.pldatabase Code=3 
"An error occured parsing the provided SQL statement." UserInfo=0x7ae7b660 
{com.plausiblelabs.pldatabase.error.vendor.code=1, NSLocalizedDescription=An error occured parsing the provided SQL statement., 
com.plausiblelabs.pldatabase.error.query.string=SELECT * FROM Events WHERE alloy_id = "62243", com.plausiblelabs.pldatabase.error.vendor.string=no such column: alloy_id} (SQLite #1: no such column: alloy_id) (query: 'SELECT * FROM Events WHERE alloy_id = "62243"')

如何调整配置以正确识别idAttribute?

2 个答案:

答案 0 :(得分:0)

使用fetch函数时,请使用eventId,而不是使用id。这样就可以查询正确的列。

答案 1 :(得分:0)

因为你已经在模型中定义了idAttribute(Events.js),试试这个:

var events = Alloy.createCollection('Events');
events.fetch({ idAttribute : "62243" });

它应该在模型的适配器部分读取你的idAttribute的值,因为在build / android / assets / alloy / sync / sql.js中你可以找到这个

...(o+=" WHERE "+(t.idAttribute?t.idAttribute:ALLOY_ID_DEFAULT)+" = "+(_.isString(i.id)?'"'+i.id+'"':i.id))...

因此,如果参数数组中没有idAttribute,则使用alloy_id