Sequelizejs 关联

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

内容简介:看似一对一,其实一对多.这里的 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: '',
      ...
    }
  }]
}]

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

查看所有标签

猜你喜欢:

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

数字民主的迷思

数字民主的迷思

[美] 马修·辛德曼 / 唐杰 / 中国政法大学出版社 / 2015-12-25 / CNY 39.00

马修·辛德曼著的《数字民主的迷思》主要讨论互联网对美国政治的影响,聚焦的是“民主化”这一课题。针对公众关于网络民主的美好想象与过分狂热,它通过对在线竞选、链接结构、流量模式、搜索引擎使用、博客与博主、内容生产的“规模经济”等主题的深入处理,借助大量数据图表与分析,勾勒出互联网政治的种种局限性。尤其表明,网络政治信息仍然为一小群精英与机构所创造和过滤,在网络的每一个层次和领域都仍然遵循着“赢家通吃”......一起来看看 《数字民主的迷思》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具