Search code examples
loopbackjsloopbackloopback4

Loopback 4: How to access table with underscore in name from loopback 4?


I am trying to get data from my data source using loopback 4. It is working fine if the table name is simple and does not contain any special character.
But if there is a table with some special character like Underscore it does not allow me to create a model for that and I am not able to access the data from that table.

I have a table named "my_data" that contains column:- id,first_name,last_name. But when I use the command lb4 model and pass the model name as my_data it converts it to my-data. and later on, when I call the API it throws an error by saying that relation publi.mydata does not exist.

WARNING: relational database doesn't support {strict: false} mode. {strict: true} mode will be set for model MyData instead.
Unhandled error in GET /my_data?filter=%7B%0A%20%20%22fields%22%3A%20%7B%0A%20%20%20%20%22id%22%3A%20true%2C%0A%20%20%20%20%first_name%22%3A%20true%2C%0A%20%20%20%20%22additionalProp1%22%3A%20%7B%7D%0A%20%20%7D%0A%7D: 500 error: relation "public.mydata" does not exist
    at Connection.parseE (/Users/apple/others/loopback4/hello/node_modules/pg/lib/connection.js:614:13)
    at Connection.parseMessage (/Users/apple/others/loopback4/hello/node_modules/pg/lib/connection.js:413:19)
    at Socket.<anonymous> (/Users/apple/others/loopback4/hello/node_modules/pg/lib/connection.js:129:22)
    at Socket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:273:9)
    at Socket.Readable.push (_stream_readable.js:214:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:186:23)

Is ther any way to get data from table named like this? If anyone know how to do this please let me know.


Solution

  • Use settings.table in the @model decorator:

    @model({
      settings: {
        table: 'my_data',
      },
    })
    

    Further reading