2016-02-16 12 views
0

我想在Loopback中自动生成模型的ID,以便第一个ID是1001,然后是1002,1003 ...等等。重要的是,所有ID都超过1000.在Loopback中自动生成以1000开头的ID

我已经尝试在保存和保存操作挂钩之前添加一个千位id值,但在挂钩之前id属性没有定义,并在挂钩后的变化是没有保存。我正在使用postgresql。

我该怎么做?在externalorder.json

我的模型的定义是:

{ 
    "name": "Externalorder", 
    "base": "PersistedModel", 
    "idInjection": false, 
    "options": { 
    "validateUpsert": true 
    }, 
    "postgresql": { 
    "schema": "public", 
    "table": "externalorder" 
    }, 
    "properties": { 
    "externalorderId": { 
     "type": "number", 
     "id": true, 
     "generated": true, 
     "required": false, 
     "length": null, 
     "precision": 32, 
     "scale": 0, 
     "postgresql": { 
     "columnName": "externalorder_id", 
     "dataType": "integer", 
     "dataLength": null, 
     "dataPrecision": 32, 
     "dataScale": 0, 
     "nullable": "NO" 
     }, 
     "_selectable": false, 
     "comments": "tilausnumero" 
    }, 
... 

回答

0

而不是使用从0生成默认的ID,您可以使用全球唯一的ID。那些大串,总是独一无二的。也许你也可以使用type:number作为id,从不测试它。

{ 
    "name": "Externalorder", 
    "base": "PersistedModel", 
    "idInjection": true, // Set to true 
    "options": { 
    "validateUpsert": true 
    }, 
    "postgresql": { 
    "schema": "public", 
    "table": "externalorder" 
    }, 
    "properties": { 
    "containerId": { 
     "defaultFn" : "guid", // use globally unique id 
     "type": "string" 
    }, 
+0

谢谢你的回答,虽然它不能真正解决我的问题,因为我特别需要以1000开头的id号。 – ritvje