我有两个型号。用户和管理者
用户模型
const UserMaster = sequelize.define('User', {
UserId: {
type: DataTypes.BIGINT,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
RelationshipId: {
type: DataTypes.STRING,
allowNull: true,
foreignKey: true
},
UserName: {
type: DataTypes.STRING,
allowNull: true
}
})
Run Code Online (Sandbox Code Playgroud)
经理模型
const Manager = sequelize.define('Manager', {
ManagerId: {
type: DataTypes.BIGINT,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
RelationshipId: {
type: DataTypes.STRING,
allowNull: true,
foreignKey: true
},
MangerName: {
type: DataTypes.STRING,
allowNull: true
}
})
Run Code Online (Sandbox Code Playgroud)
模型被缩小以简化问题
协会..
User.belongsTo(models.Manager, {
foreignKey: 'RelationshipId',
as: 'RM'
});
Manger.hasMany(model.User, { …Run Code Online (Sandbox Code Playgroud) 我有三个 json 模式定义。客户、地址和联系方式。
客户端.json
{
"$id": "client.json",
"type": "object",
"definitions": {},
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {
"name": {
"$id": "/properties/name",
"type": "string"
},
"id": {
"$id": "/properties/id",
"type": "integer"
},
"contact": {
"$ref": "contact.json"
},
"address": {
"$ref": "address.json"
}
}
}
Run Code Online (Sandbox Code Playgroud)
地址.json
{
"$id": "address.json",
"type": "array",
"definitions": {},
"$schema": "http://json-schema.org/draft-06/schema#",
"items": {
"$id": "/items",
"type": "object",
"properties": {
"addressId": {
"$id": "/items/properties/addressId",
"type": "integer"
},
"addressName": {
"$id": "/items/properties/addressName",
"type": "string"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
联系人.json
{
"$id": "contact.json", …Run Code Online (Sandbox Code Playgroud)