How do I get the Last Insert Id from a JayData transaction?

99封情书 提交于 2019-12-23 02:58:06

问题


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

回答1:


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().

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

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.

Does it work?



来源:https://stackoverflow.com/questions/14739212/how-do-i-get-the-last-insert-id-from-a-jaydata-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!