Titanium JS:尝试应用数据库迁移时出错

时间:2014-06-29 16:14:35

标签: sqlite migration titanium appcelerator titanium-alloy

我在models/beers.js文件中添加了一个新列,需要为我的应用的当前用户进行数据库迁移。我根据documentation创建了一个迁移,但现在当我在模拟器中运行我的应用程序时,出现以下错误:

[ERROR] :  Script Error {
[ERROR] :      backtrace = "#0 Migrate() at file:///Users/owen/Library/Application%20Support/iPhone%20Simulator/7.1-64/Applications/2BAE6ECA-8995-4E0F-BA2F-9CF00162E81E/Beer%20Pad.app/alloy/sync/sql.js:245\n#1 () at file:///Users/owen/Library/Application%20Support/iPhone%20Simulator/7.1-64/Applications/2BAE6ECA-8995-4E0F-BA2F-9CF00162E81E/Beer%20Pad.app/alloy/sync/sql.js:329\n#2 () at file:///Users/owen/Library/Application%20Support/iPhone%20Simulator/7.1-64/Applications/2BAE6ECA-8995-4E0F-BA2F-9CF00162E81E/Beer%20Pad.app/alloy.js:95\n#3 () at file:///Users/owen/Library/Application%20Support/iPhone%20Simulator/7.1-64/Applications/2BAE6ECA-8995-4E0F-BA2F-9CF00162E81E/Beer%20Pad.app/alloy/models/Beers.js:100";
[ERROR] :      line = 72;
[ERROR] :      message = "invalid SQL statement";
[ERROR] :      nativeLocation = "-[TiDatabaseProxy execute:] (TiDatabaseProxy.m:191)";
[ERROR] :      nativeReason = "Error Domain=com.plausiblelabs.pldatabase Code=3 \"An error occured parsing the provided SQL statement.\" UserInfo=0xfa7d920 {com.plausiblelabs.pldatabase.error.vendor.code=1, NSLocalizedDescription=An error occured parsing the provided SQL statement., com.plausiblelabs.pldatabase.error.query.string=ALTER TABLE beers ADD COLUMN is_sample BOOLEAN;, com.plausiblelabs.pldatabase.error.vendor.string=no such table: beers}";
[ERROR] :      sourceId = 351886816;
[ERROR] :      sourceURL = "file:///Users/owen/Library/Application%20Support/iPhone%20Simulator/7.1-64/Applications/2BAE6ECA-8995-4E0F-BA2F-9CF00162E81E/Beer%20Pad.app/alloy/models/Beers.js";
[ERROR] :  }
[ERROR] :  Script Error Module "alloy/models/Beers" failed to leave a valid exports object

这是我的模型文件代码

exports.definition = {
config: {
    columns: {
        "name": "text",
        "brewery": "text",
        "rating": "integer",
        "percent": "integer",
        "establishment": "text",
        "location": "text",
        "notes": "text",
        "date": "text",
        "date_string": "text",
        "beer_image": "text",
        "latitude": "integer",
        "longitude": "integer",
        "favourite": "boolean",
        "is_sample": "boolean"  // this is the new column
    },
    adapter: {
        type: "sql",
        collection_name: "beers"
    }
}
/* and so on ... */

以下是我最近添加的迁移文件:

201405291604237_Beers.js

migration.up = function(migrator) {
    migrator.db.execute('ALTER TABLE ' + migrator.table + ' ADD COLUMN is_sample BOOLEAN;');
};

migration.down = function(migrator) {
    var db = migrator.db;
    var table = migrator.table;
    db.execute('CREATE TEMPORARY TABLE beers_backup(alloy_id,name,brewery,rating,percent,establishment,location,notes,date,date_string,beer_image,latitude,longitude,favourite);');
    db.execute('INSERT INTO beers_backup SELECT alloy_id,name,brewery,rating,percent,establishment,location,notes,date,date_string,beer_image,latitude,longitude,favourite FROM ' + table + ';');
    migrator.dropTable();
    migrator.createTable({
        columns: {
            "name": "text",
            "brewery": "text",
            "rating": "integer",
            "percent": "integer",
            "establishment": "text",
            "location": "text",
            "notes": "text",
            "date": "text",
            "date_string": "text",
            "beer_image": "text",
            "latitude": "integer",
            "longitude": "integer",
            "favourite": "boolean"
        },
    });
    db.execute('INSERT INTO ' + table + ' SELECT alloy_id,name,brewery,rating,percent,establishment,location,notes,date,date_string,beer_image,latitude,longitude,favourite FROM beers_backup;');
    db.execute('DROP TABLE beers_backup;');
};

那么,我在哪里错了?我已经按照文档中的示例和一些教程进行了操作。

2 个答案:

答案 0 :(得分:1)

阅读错误消息。

  

NSLocalizedDescription =解析提供的SQL时出错   声明。,com.plausiblelabs.pldatabase.error.query.string = ALTER   表啤酒ADD COLUMN is_sample BOOLEAN;,   com.plausiblelabs.pldatabase.error.vendor.string =没有这样的表:   啤酒}&#34 ;;

你正在尝试ALTER TABLE啤酒,但没有这样的表。

答案 1 :(得分:0)

我通过添加初始迁移解决了这个问题。我创建了另一个迁移,它的文件名前面有一个较旧的日期时间代码,然后添加了以下内容:

migration.up = function(migrator) {
    migrator.createTable({
        columns: {
            "name": "text",
            "brewery": "text",
            "rating": "integer",
            "percent": "integer",
            "establishment": "text",
            "location": "text",
            "notes": "text",
            "date": "text",
            "date_string": "text",
            "beer_image": "text",
            "latitude": "integer",
            "longitude": "integer",
            "favourite": "boolean"
        },
        adapter: {
            type: "sql",
            collection_name: "beers"
        }
    });
};

migration.down = function(migrator) {
    migrator.dropTable("beers");
};

我不确定为什么我需要这样做,因为我认为创建一个模型文件会处理表的初始创建,但这有效并解决了我的问题!