Sequelizejs 关联

栏目: ASP.NET · 发布时间: 5年前

内容简介:看似一对一,其实一对多.这里的 One-To-One 指的应该是查询数据(主表)结果中,关联信息是以实际上是,主表与关联表的多对一关系.查询出来结果结构如:

One-To-One

看似一对一,其实一对多.这里的 One-To-One 指的应该是查询数据(主表)结果中,关联信息是以 单个 形式作为一个属性挂在主表 每一个 对象中

实际上是,主表与关联表的多对一关系.

belongsTo

SourceModel.belongsTo(TargetModel, { as, foreignKey, targetKey })

拿 SourceModel 中的 foreignKey 和 TargetModel 中的 targetKey 进行关联.

as 配置 TargetModel 的别名

foreignKey 配置 SourceModel 中的外键字段名称,默认为 ${as || TargetModel.name}+${TargetModel.primaryKey}

targetKey 配置 TargetModel 中的目标键字段名称,默认为 TargetModel 主键

查询出来结果结构如:

const sourceObj = {
  <sourceObjAttr>: <value>,
  <sourceObjAttr>: <value>,
  ...
  <as || TargetModel.name>: targetObj
}

SourceModel 中存在 foreignKey 关联 TargetModel,比如:

Player.belongsTo(Team)

foreignKey 用来自定义 SourceModel 中的外键名称.比如:

User.belongsTo(Company, { foreignKey: 'cid' }) // User.cid 即外键

默认情况下,foreignKey 值为 ${Team.name}+${Team.primaryKey} .注意这里的连接是根据 source model 的配置而决定是 camelCase 还是 underscored.例如:

const User = this.sequelize.define('user', {}, { underscored: true })
const Company = this.sequelize.define('company', {
  uuid: { type: Sequelize.UUID, primaryKey: true }
})

User.belongsTo(Company) // User 的 foreignKey 为 company_uuid

targetKey 用来指定 targetModel 中用来和 sourceModel 中外键进行匹配的字段名称,默认为 targetModel 中的 primaryKey.比如:

User.belongsTo(Company, { foreignKey: 'cid', targetKey: 'uuid' }) // 即将 User.cid 与 Company.uuid 进行关联

as 用来转换 targetModel 的 name. 这里有两个方面,一个是在生成 foreignKey 时替换 targetModel name,一个是在拼装查询结果时,改变 targetModel 作为 sourceModel 中的属性的名称.比如:

User.belongsTo(Company, { as: 'com' })

// foreign key would be: com_uuid
// company property of user object would be 'com'

hasOne

SourceModel.hasOne(TargetModel, { as, foreignKey })

拿 SourceModel 中的主键和 TargetModel 中的外键进行关联

as 配置 TargetModel 的别名

foreignKey 配置 TargetModel 中的外键字段名称,默认为 ${as || SourceModel.name}+${SourceModel.primaryKey}

查询结构为:

const sourceObj = {
  <sourceObjAttr>: <value>,
  <sourceObjAttr>: <value>,
  ...
  <as || TargetModel.name>: targetObj
}

TargetModel 中存在 foreignKey 关联 SourceModel,比如:

Company.hasOne(User)

foreignKey 默认为 ${Company.name}+${Company.primaryKey} .也可以自定义:

Company.hasOne(User, { foreignKey: 'company_id' }) // User.company_id 为外键

targetKey hasOne 中没有 targetKey 属性

belongsTo V.S hasOne

如果关联信息(比如:外键)保存在 source 中,就是用 belongsTo ;如果关联信息保存在 target 中,则是用 hasOne

比如:

  1. Player 表,teamId 关联 Team 表
  2. Coach 表
  3. Team 表,coachId 关联 Coach 表
Player.belongsTo(Team) // Player.teamId -- Team.id
Coach.hasOne(Team) // Team.coachId -- Coach.id

为什么不能反过来,比如 Player.belongsTo(Team) 中将 source 和 target 交换下,不就可以应用 hasOne 了吗?问题就在于 source 是根据查询要求而定 的,如果是要查询 Player,并把关联的 Team 信息带出来,那么 source 就只能是 Player.

小结:根据查询主表,即 source,找到需要查询到的关联表,如果关联信息(外键)在 source 中,则 belongsTo,否则 hasOne.

One-To-Many

hasMany

SourceModel.hasMany(TargetModel, { as, foreignKey, sourceKey })

拿 TargetModel 中的外键与 SourceModal 中的主键关联

as 配置 TargetModel 别名

foreignKey 配置 TargetModel 中外键名称,默认为 ${SourceModel.name}+${SourceMode.primaryKey}

soruceKey 配置 SourceModel 中关联键名称,默认为 SourceModel.primaryKey

这里 as 的值并不影响 key 的默认值

Many-To-Many

belongsToMany

SourceModel.belongsToMany(TargetModel, { through: AssociationModel, as, foreignKey, otherKey })

通过中间表进行关联

as 中间表 AssociationModel 的别名

foreignKey 配置 AssociationModel 中与 SourceModel 主键关联的外键名称,默认为 SourceModel.name + SourceModel.primaryKey

otherKey 配置 AssociationModel 中与 TargetModel 主键关联的外键名称,默认为 TargetModel.name + TargetModel.primaryKey

这里 as 的值并不影响 key 的默认值

User.belongsToMany(Role, { through: RoleAssignment, foreignKey: 'userId', otherKey: 'roleId' })

user.findAll({
  include: [{
    model: Role,
    through: {
      // attributes: [], // uncomment this line to hide RoleAssignment as attribute in result in target model
      model: RoleAssignment,
      where
    }
  }]
})

返回结果

const result = [{
  id: '',
  userName: '',
  gender: '',
  roles: [{
    id: '',
    roleName: '',
    ...
    RoleAssignment: {
      id: '',
      roleId: '',
      userId: '',
      ...
    }
  }]
}]

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

UML参考手册

UML参考手册

兰博 / UML China / 机械工业出版社 / 2005-8 / 75.00元

《UML参考手册》在第1版的基础上进行了重大更新和扩展。UML的创建者James Rumbaugh、Ivar Jacobson和Grady Booch,清晰完整地讲述了UML的所有概念,包括对序列图、活动模型、状态机、组件、类和组件的内部结构以及特性描述的主要修订。手册式结构不仅有助于读者对UML的概念进行规范化的学习与理解,更为广大程序开发人员、系统用户和工程技术人员提供了方便快捷的查询方式。无......一起来看看 《UML参考手册》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具