Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

loopbackjs

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.

like image 266
fusionstrings Avatar asked Jan 02 '14 06:01

fusionstrings


People also ask

How do you create a relation in loopback?

The easiest way to create a new relation between existing models is to use the slc loopback:relation , or the apic loopback:relation , relation generator. The tool will prompt you to enter the type of relation (belongsTo, hasMany, and so on) and the affected models.

What is loopback model?

A LoopBack model is a JavaScript object with both Node and REST APIs that represents data in backend systems such as databases. Models are connected to backend systems via data sources. You use the model APIs to interact with the data source to which it is attached.


1 Answers

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

like image 51
fusionstrings Avatar answered Sep 22 '22 00:09

fusionstrings