ORMLight 升级数据库我如何删除自动增量并插入新的 foreigncollection
ORMLight Upgrade Database how do i remove autoincrement and insert new foreigncollection
我正在尝试升级 ormlite/sqlite android 数据库。
我有一个 java class 这样的:
@DatabaseTable
public class Assessment {
@DatabaseField(generatedId = true)
private Long assessmentId;
@DatabaseField
private java.lang.String assessmentName;
...
}
并希望它像这样改变:
@DatabaseTable
public class Assessment {
//Change from autoincrement to insertable id
@DatabaseField(id = true)
private Long assessmentId;
@DatabaseField
private java.lang.String assessmentName;
//NEW FIELD
@ForeignCollectionField
private java.util.Collection<AssessmentEntry> assessmentEntries;
...
}
我创建了一个新模型class:
@DatabaseTable
public class AssessmentEntry implements Comparable<AssessmentEntry>{
@DatabaseField(id = true)
private Long AssessmentEntryId;
@DatabaseField
private String entryName;
@DatabaseField
private int orderNumber;
@DatabaseField(foreign = true)
private Assessment assessment;
...
}
我还使用 OrmLiteConfigUtil 创建映射文件(如果相关)。
现在我正尝试通过 onUpgrade(...)
将这些更改添加到数据库
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVersion, int newVersion) {
if(oldVersion < 2) {
try {
//Create new table
TableUtils.createTable(connectionSource, AssessmentEntry.class);
//implement changes for class Assessment
} catch (SQLException e) {
e.printStackTrace();
}
}
}
所以我的问题是:我是否需要通过原始查询更改评估 table 以应用从 generatedId 到 id 的更改?由于 table 评估中的外国 collection,我是否需要对 ormlite 进行任何特定更改?
So my question is: do I need to change the assessment table via raw query to apply the change from generatedId to id?
是的,你知道。我会比较 sqlite 输出以查看它在那里的期望。您可能需要创建一个新的 assessmentId2
列,为其分配当前 assessmentId
的值,然后删除当前的并将新的重命名回 assessmentId
。
Do i need to make any ormlite specific changes because of the foreign collection in table assessment?
没有。 foreign-collection 想要的只是一个 ID。它是如何创建的并不重要。
我正在尝试升级 ormlite/sqlite android 数据库。
我有一个 java class 这样的:
@DatabaseTable
public class Assessment {
@DatabaseField(generatedId = true)
private Long assessmentId;
@DatabaseField
private java.lang.String assessmentName;
...
}
并希望它像这样改变:
@DatabaseTable
public class Assessment {
//Change from autoincrement to insertable id
@DatabaseField(id = true)
private Long assessmentId;
@DatabaseField
private java.lang.String assessmentName;
//NEW FIELD
@ForeignCollectionField
private java.util.Collection<AssessmentEntry> assessmentEntries;
...
}
我创建了一个新模型class:
@DatabaseTable
public class AssessmentEntry implements Comparable<AssessmentEntry>{
@DatabaseField(id = true)
private Long AssessmentEntryId;
@DatabaseField
private String entryName;
@DatabaseField
private int orderNumber;
@DatabaseField(foreign = true)
private Assessment assessment;
...
}
我还使用 OrmLiteConfigUtil 创建映射文件(如果相关)。
现在我正尝试通过 onUpgrade(...)
将这些更改添加到数据库@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVersion, int newVersion) {
if(oldVersion < 2) {
try {
//Create new table
TableUtils.createTable(connectionSource, AssessmentEntry.class);
//implement changes for class Assessment
} catch (SQLException e) {
e.printStackTrace();
}
}
}
所以我的问题是:我是否需要通过原始查询更改评估 table 以应用从 generatedId 到 id 的更改?由于 table 评估中的外国 collection,我是否需要对 ormlite 进行任何特定更改?
So my question is: do I need to change the assessment table via raw query to apply the change from generatedId to id?
是的,你知道。我会比较 sqlite 输出以查看它在那里的期望。您可能需要创建一个新的 assessmentId2
列,为其分配当前 assessmentId
的值,然后删除当前的并将新的重命名回 assessmentId
。
Do i need to make any ormlite specific changes because of the foreign collection in table assessment?
没有。 foreign-collection 想要的只是一个 ID。它是如何创建的并不重要。