【问题标题】:Titanium JS: Error trying to apply a database migrationTitanium JS:尝试应用数据库迁移时出错
【发布时间】:2014-06-29 16:14:35
【问题描述】:

我在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;');
};

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

【问题讨论】:

    标签: sqlite migration titanium appcelerator titanium-alloy


    【解决方案1】:

    阅读错误信息。

    NSLocalizedDescription=解析提供的 SQL 时发生错误 声明。,com.plausiblelabs.pldatabase.error.query.string=ALTER TABLE beers ADD COLUMN is_sample BOOLEAN;, com.plausiblelabs.pldatabase.error.vendor.string=没有这样的表: 啤酒}";

    您正在尝试ALTER TABLE beers,但没有这样的表。

    【讨论】:

    • 迁移应该应用于现有数据库。问题是为什么桌子不在那里。
    • 因为没有创建表?很难说...可能会发生更多事情。
    • 谢谢 0101,看来我也必须创建一个初始迁移,它会创建原始表。我不知道为什么,因为我认为模型文件已经解决了这个问题,但我还是将它添加为答案,因为它有效。
    【解决方案2】:

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

    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");
    };
    

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

    【讨论】:

      猜你喜欢
      • 2015-10-14
      • 1970-01-01
      • 2019-02-14
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      • 2019-01-12
      • 1970-01-01
      相关资源
      最近更新 更多