Search code examples
loopbackjs

Loopback model definition is not adding foreign key relation in database table


I am using loopback for API design and data modeling. I am using MySQL as my database. Although my API rest URL successfully returns results e.g. /states/{id}/cities. I have following model but it seems no foreign key relation is added. Following are my model definition.

"state": {
  "options": {
    "relations": {
      "cities": {
        "type": "hasMany",
        "model": "city",
        "foreignKey": "stateId"
      }
    }
  },
  "properties": {
    "name": {
      "type": "string"
    }
  },
  "public": true,
  "dataSource": "db",
  "plural": "states"
},
"city": {
  "options": {
    "relations": {
      "state": {
        "type": "belongsTo",
        "model": "state",
        "foreignKey": "stateId"
      }
    }
  },
  "properties": {
    "name": {
      "type": "string"
    }
  },
  "public": true,
  "dataSource": "db",
  "plural": "cities"
}

and below is screenshot of city table. enter image description here

And following is State table screenshot. enter image description here

I may be doing it wrong here. Looking forward for any pointers.


Solution

  • It seems Loopback handles relations in models using "WHERE" query and not based on relations. Following are details.

    https://github.com/strongloop/loopback-connector-mysql/issues/16