如何在应用程序制造商中从 5000 开始自动增加字段
How to make auto-increment field in app maker from 5000
我有 table 个产品,order_ID
列需要从 5000 开始
我试过这个:
if (record.Order_ID) {
record.Order_ID = record.Order_ID + 1;
}
else {
record.Order_ID = 5000;
}
如果您需要使用 Drive Tables 来实现它:
// onCreate model's event handler
// it receives about-to-create record as parameter
var ID_START_FROM = 5000;
var lock = LockService.getScriptLock();
// we need to lock our script, to prevent IDs inconsistency
// wait 3 seconds for script lock
lock.waitLock(3000);
// Run query to get latest ID
var query = app.models.Product.newQuery();
query.sorting.Id._descending();
query.limit = 1;
var records = query.run();
var next_id = records.length > 0 ? records[0].Id + 1 : ID_START_FROM;
record.Id = next_id;
// ...do other stuff
lock.releaseLock();
如果您使用云 SQL 作为数据后端,您可以轻松更改 table:
ALTER TABLE Product AUTO_INCREMENT = 5000;
我有 table 个产品,order_ID
列需要从 5000 开始
我试过这个:
if (record.Order_ID) {
record.Order_ID = record.Order_ID + 1;
}
else {
record.Order_ID = 5000;
}
如果您需要使用 Drive Tables 来实现它:
// onCreate model's event handler
// it receives about-to-create record as parameter
var ID_START_FROM = 5000;
var lock = LockService.getScriptLock();
// we need to lock our script, to prevent IDs inconsistency
// wait 3 seconds for script lock
lock.waitLock(3000);
// Run query to get latest ID
var query = app.models.Product.newQuery();
query.sorting.Id._descending();
query.limit = 1;
var records = query.run();
var next_id = records.length > 0 ? records[0].Id + 1 : ID_START_FROM;
record.Id = next_id;
// ...do other stuff
lock.releaseLock();
如果您使用云 SQL 作为数据后端,您可以轻松更改 table:
ALTER TABLE Product AUTO_INCREMENT = 5000;