0
votes

I am migrating from using mongodb to postgres in loopback 3. When I was using mongodb, auto generated ids were of type string. In case of postgres, auto generated id is of type number.

I have gone through the loopback documentation, it says:

The generated property indicates the ID will be automatically generated by the database. If true, the connector decides what type to use for the auto-generated key. For relational databases, such as Oracle or MySQL, it defaults to number.

So, according to the documentation, for relational databases, default id is of type number. How can I change this default to of type string?

2

2 Answers

0
votes

I think you can override the id property in the declaration of the model. Just check that you have set the idInjection property to false. Then, declare a property named "Id" and set the data type you want. Of course, you should migrate the model again.

0
votes

You can use the defaultFn to setup a default string ID, here is my sample code using uuidv4 to generate random string ID

"userId": {
        "type": "string",
        "id": true,
        "length": 36,
        "defaultFn": "uuidv4",
        "dataType": "char"
    }