- 简介
- 定义 HasOne 关系
- 添加方法到 HasOne 的模型
使用命令行工具
案例, 供应商 和 账号信息。
一个供应商有一个帐户, 在 account Model 中申明外键 supplierId
{
"name": "account",
"base": "PersistedModel",
"properties": {
"id": {
"required": true
},
"acctmgr": {
"type": "string"
},
"supplierId": {
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": []
}
如果不指定foreignKey(外键), LoopBack 派生关系的名称和外键规则如下:
- Relation name: 驼峰化 Model Name + 复数格式(默认s) ,如 Order => orders
一旦定义了 hasOne 关系,Loopback将添加对应的方法到 Model 的原型对象 如: