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.
And following is State table screenshot.
I may be doing it wrong here. Looking forward for any pointers.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With