小编Ksh*_*esh的帖子

从sequelize.js 中的关联模型加载属性

我有两个型号。用户和管理者

用户模型

 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)

sql orm node.js sequelize.js

6
推荐指数
1
解决办法
1万
查看次数

$ref 不适用于数组类型 json 模式

我有三个 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)

schema json jsonschema ajv

2
推荐指数
1
解决办法
2657
查看次数

标签 统计

ajv ×1

json ×1

jsonschema ×1

node.js ×1

orm ×1

schema ×1

sequelize.js ×1

sql ×1