如何从JayData事务中获取最后插入ID? [英] How do I get the Last Insert Id from a JayData transaction?
问题描述
使用JayData将新对象(记录)插入Sqlite数据库中,如何查询最后一个插入ID?我可以这样做,但看起来不太优雅:
Using JayData to insert a new object (record) into a Sqlite database, how do I query for the last insert id? I can do it like this but it seems inelegant:
DB.Members.orderByDescending('member.Id').take(1).toArray(function(member){
alert(member.Name);
});
推荐答案
如果您使用自动递增的ID定义了Member类,则无需编写其他查询,因为在 saveChanges().
If you have defined the Member class with auto-incremented Id, there is no additional query you have to write, because the Id property is filled automatically after the saveChanges().
要为您的班级定义一个自动递增的ID,请检查您是否已编写以下代码:
To define a auto-incremented Id to your class, check if you have written this:
$data.Entity.extend("Member", {
Id: { type: "int", key: true, computed: true },
...
});
var member = new Member();
member.Name = "a";
DB.Members.add(member);
DB.Members.saveChanges(function(){alert(member.Id);});
如果手动设置了ID,则您的查询仅在应用程序的单个用户手动插入记录的情况下才有效. 如果您同步在线数据存储中的数据,我们必须共同制定解决方案.
If the Id is set manually, your query is valid only in a scenario where the records are inserted manually by a single user of the app. We have to work out a solution together if you sync data from an online datastore.
行得通吗?
这篇关于如何从JayData事务中获取最后插入ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!