如何在Sequelize中消除相同模型之间的多个关联之间的歧义 [英] How to disambiguate between multiple associations between the same models in Sequelize

查看:104
本文介绍了如何在Sequelize中消除相同模型之间的多个关联之间的歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个模型-BookUserInstitution,它们彼此关联,如下所示:

I have three models — Book, User and Institution — which are associated to one another as follows:

  • 书籍通过Book_Institution联接表与机构关联(多对多关系)

  • Books are associated to Institutions via a Book_Institution join table (many to many relationship)

Book.belongsToMany(models.Institution, { through: 'Book_Institution' })

Institution.belongsToMany(models.Book, { through: 'Book_Institution' })

  • 可以通过两种方式将用户与机构建立关联:以读者或作者的身份.这是通过两个联接表完成的:Author_InstitutionReader_Institution:

    Institution.belongsToMany(models.User, { through: 'Author_Institution' })
    Institution.belongsToMany(models.User, { through: 'Reader_Institution' })
    

    User.belongsToMany(models.Institution, { through: 'Author_Institution' })
    User.belongsToMany(models.Institution, { through: 'Reader_Institution' })
    

    (为简洁起见,每次都省略foreignKey.)

    (Each time leaving out foreignKey for brevity.)

    我想查询Book模型以查找属于作者的所有书籍. Sequelize提供了include选项,可以轻松地连接两个关联的表.我正在努力解决的问题是,使用如下所示的include默认为Reader_Institution关联.如何指定应使用哪个关联?

    I want to query the Book model to find all books that belong to an author. Sequelize provides the include option to easily join two associated tables. The problem I’m stuggling with is that using include as shown below defaults to the Reader_Institution association. How can I specify which association should be used?

    getBooks: (obj, args, context) => {
      const { user } = context
    
      return Book.findAll({
        attributes: ['id', 'path'],
        include: [{
          include: [{
            attributes: ['id'],
            model: User,
            where: { id: user }
          }],
          model: Institution,
          required: true // inner join
        }]
      })
    }
    

    预先感谢您的帮助.

    推荐答案

    我使用as,它允许您通过该别名引用关系.

    I use as which allows you to reference the relationship through that alias.

    Institution.belongsToMany(models.User, { 
        through: 'Author_Institution', // many-to-many relationship table name
        as: 'AuthorInstitution' // alias
    })
    

    通过这种方式设置模型后,您可以使用as指定要在查询时包括的关系.

    With your models set up this way, you can use as to to specify which relationship you want to include when querying.

    getBooks: (obj, args, context) => {
      const { user } = context
    
      return Book.findAll({
        attributes: ['id', 'path'],
        include: [{
          include: [{
            attributes: ['id'],
            model: User,
            where: { id: user },
            as: 'AuthorInstitution'
          }],
          model: Institution,
          required: true // inner join
        }]
      })
    }
    

    此外,使用这种方法,它使您可以通过as引用关系数据,因此可以执行book.AuthorInstitution,它将成为该对象的值.

    Also, with this methodology, it allows you you to reference the relationship data via the as, so you can do book.AuthorInstitution and it will be the value of that object.

    这篇关于如何在Sequelize中消除相同模型之间的多个关联之间的歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆