我的應用程序我有一些模型,需要添加一個新的列。Appcelerator合金移植拋出SQL錯誤,當新應用程序被安裝
因此,根據documentation,我編寫了一個帶有SQL「alter table add column ..」的遷移文件,並將該屬性添加到合金模型文件中。像預期的一樣,這工作完美。
但是,當應用程序首次安裝在設備上時,會引發SQL錯誤,說我的遷移嘗試添加的列已經存在。由於數據庫模式是從模型文件創建的,我猜這個例外是正確的,但我想知道如何完成現有和新安裝的APPS的數據庫更改。刪除遷移文件,只需將該屬性添加到模型文件,將使其適用於全新安裝,但不適用於更新。
最好的問候, 斯文
更新1: 我嘗試添加一個初始遷移,而新的字段創建表,然後加在另一個移民新領域(見雷的答案)。還是一樣的錯誤。
Appcelerator的版本:5.2.2
型號適配器類型:sqlrest
更新2(一些代碼):
型號:
config: {
URL: Alloy.Globals.jsonEndPoint + Alloy.Globals.jsonRequestParams + "foto",
columns:{
id: "INTEGER PRIMARY KEY AUTOINCREMENT",
dateiname: "TEXT",
beschreibung: "TEXT",
primaerfoto: "TEXT",
aufnahmedatum: "TEXT",
anlage_id: "INTEGER",
foto_label_id: "INTEGER",
latest_sync_date: "TEXT",
dirty: "INTEGER",
begehungsbericht_protokoll_id: "INTEGER",
begehungsbericht_protokoll_server_id: "INTEGER",
},
adapter: {
remoteBackup: false, //Prevent database from being saved in icloud
db_name: this.Alloy.Globals.currentDatabase,
type: "sqlrest",
collection_name: "foto",
idAttribute: "id"
}
遷移1:
migration.up = function(migrator) {
Ti.API.info(">>>>>>>>>>>>>>>> migrate create table UP <<<<<<<<<<<<<");
migrator.createTable({
columns: {
id: "INTEGER PRIMARY KEY AUTOINCREMENT",
dateiname: "TEXT",
beschreibung: "TEXT",
primaerfoto: "TEXT",
aufnahmedatum: "TEXT",
anlage_id: "INTEGER",
foto_label_id: "INTEGER",
latest_sync_date: "TEXT",
dirty: "INTEGER",
begehungsbericht_protokoll_id: "INTEGER",
}
});
遷移2:
migration.up = function(migrator) {
Ti.API.info(">>>>>>>>>>>>>>>> migrate ALTER table UP <<<<<<<<<<<<<");
migrator.db.execute('ALTER TABLE foto ADD COLUMN begehungsbericht_protokoll_server_id INTEGER;');
};
更新3(與解決方法解決方案): 因爲我知道我運行使用該信息爲條件加入列(如塞薩爾提議)遷移時,該表應具有的列數。
migration.up = function(migrator) {
Ti.API.info("migrating foto table");
var rows = migrator.db.execute("SELECT * FROM foto");
Ti.API.info("field count: " + rows.fieldCount);
if (rows.fieldCount < 11) {
Ti.API.info("adding column: begehungsbericht_protokoll_server_id");
migrator.db.execute('ALTER TABLE foto ADD COLUMN begehungsbericht_protokoll_server_id INTEGER');
} else {
Ti.API.info("NOT adding column: begehungsbericht_protokoll_server_id");
}
};
根據Cesar的答案添加了解決方案解決方案。不要那麼喜歡那個,但是現在確實如此。 – Sven
順便說一句,遷移1是不需要在我的情況! – Sven
我不知道SQLRest遷移是否正常工作?自定義適配器的問題在於它可能與其他方式的工作方式不同...... – Ray