Search code examples
javascriptnode.jsloopback

Loopback and default sorting


I'm starting to study loopback. I created my app, and below this model:

{
  "name": "movimenti",
  "plural": "movimenti",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "mov_id": {
      "type": "number",
      "required": true
    },
    "mov_tipo": {
      "type": "string",
      "required": true
    },
    "mov_valore": {
      "type": "number",
      "required": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

I connected the model to my MySQL DB:

"movimenti": {
    "dataSource": "banca",
    "public": true
}

I launched the application, and went to the address indicated. I questioned the GET method, having this error:

"stack": "Error: ER_BAD_FIELD_ERROR: Unknown column 'id' in 'field list'\n

but I do not have an ID field in my table. How can I fix this problem?


Solution

  • Loopback will automatically add an id column if none of the properties of a model is mentioned as id.

    Assuming for your model, property mov_id is the id. Define so in the model by adding id: true line: Reference

    {
      ...
      "properties": {
        "mov_id": {
          "type": "number",
          "required": true,
          "id":true
        },
      ...
    }