findall 多对多续集

Posted

技术标签:

【中文标题】findall 多对多续集【英文标题】:findall many to many sequelize 【发布时间】:2019-05-20 21:54:20 【问题描述】:

我有一个包含许多人表和列表的列表。我需要进行包含关系表的查询,例如:

选择 p.id, pl.id

FROM people p inner join peopletolists pl

WHERE p.id == pl.id

我的模型: 列出模型:

'use strict';
module.exports = (sequelize, DataTypes) => 
  const lists = sequelize.define('lists', 
    listId: 
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    ,
    nome: DataTypes.STRING(50),
    status: DataTypes.STRING(1),
    parametros: DataTypes.STRING(500)
  , 
    timestamps: false,
  );

  lists.associate = function(models) 
    lists.belongsToMany(models.people,  
      through: models.peopletolists, 
      foreignKey: 'listId',
    );
  

  return lists;
;

人物模型:

'use strict';
module.exports = (sequelize, DataTypes) => 
  const people = sequelize.define('people', 
    peopleId: 
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    ,
    cpf: DataTypes.STRING(11),
    name: DataTypes.STRING(50),
    city: DataTypes.STRING(50),
  , 
      timestamps: false
    );
  people.associate = function (models) 
    people.belongsToMany(models.lists,  
      through: models.peopletolists, 
      foreignKey: 'peopleId' 
    );
  

  return people;
;

N:M 型号:

'use strict';
module.exports = function (sequelize, DataTypes) 
  const peopletolists = sequelize.define("peopletolists", 
    id: 
      type: DataTypes.INTEGER,
      primaryKey: true,
      allowNull: false,
      autoIncrement: true,
      unique: true,
    ,
    listId: 
      type: DataTypes.INTEGER,
      references: 
        model: 'lists',
        key: 'listId'
      ,
      allowNull: false
    ,
    peopleId: 
      type: DataTypes.INTEGER,
      references: 
        model: 'people',
        key: 'peopleId'
      ,
      allowNull: false
    
  , 
    timestamps: false
  );

  return peopletolists;

查询:

router.get('/', function (req, res, next) 
    model.lists.findAll(
        include: [
            model: model.peopletolists,
        ]
    )
        .then(lists => res.json(
            data: lists,
        ))
        .catch(err => res.json(
            error: err,
        ))
);

错误:SequelizeEagerLoadingError

一个人有很多列表,列表也有很多人。我在 Sequelize 文档中没有找到很多东西,也没有多少人抱怨这个错误,它通常在 ClassMethods 上,但我什至没有使用它。

怎么了?

【问题讨论】:

试试required: falsethrough: attributes: []subQuery: false 不幸的是它没有用 【参考方案1】:

这就是我使用它的方式。

ArtistModel.findAll(
    where: 
      slug: req.params.slug
    , attributes: ['id']
    , include: [
      model: Genres,
      as: 'ArtistGenre',
      required: false,
      // attributes: ['id'],
      through: attributes: [],
    ],
    subQuery: false,
    limit: req.query.limit,
    offset: req.skip
  )

其中 Genre 是通过 ArtistGenre 以多对多关系与 Artist 表链接的表

艺术家流派模特:

const db = require('../utils/connection');
const Sequelize = require('sequelize');

let ArtistGenre = db.define('artist_genre', 
  id: 
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true
  
, 
  timestamps: false,
  underscored: true
);

ArtistGenre.removeAttribute('id');

module.exports = ArtistGenre;

艺术家:

const db = require('../utils/connection');
const Sequelize = require('sequelize');

module.exports = db.define('artist', 
  id: 
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true
  ,
  name: 
    type: Sequelize.STRING
  ,
  status: 
    type: Sequelize.ENUM,
    values: ['active', 'inactive'],
    defaultValue: 'active'
  ,
  created_at: 
    type: 'TIMESTAMP',
    defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
    allowNull: false
  ,
  updated_at: 
    type: 'TIMESTAMP',
    defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
    allowNull: false
  
, 
  underscored: true,
  timestamps: false
);

类型模型:

const db = require('../utils/connection');
const Sequelize = require('sequelize');

module.exports = db.define('genre', 
  id: 
    type: Sequelize.BIGINT(20),
    primaryKey: true,
    autoIncrement: true
  ,
  name: 
    type: Sequelize.STRING
  
, 
  timestamps: false
);

关联如下:

Artist.belongsToMany(models.genre, as: 'ArtistGenre', through: 'artist_genre', foreignKey: 'artist_id', otherKey: 'genre_id');
Genre.belongsToMany(models.artist, as: 'ArtistGenre', through: 'artist_genre', foreignKey: 'genre_id', otherKey: 'artist_id');

【讨论】:

你的模特怎么样? 我已经从这里的 Artist 模型中删除了某些不相关的字段并且正在增加代码大小。

以上是关于findall 多对多续集的主要内容,如果未能解决你的问题,请参考以下文章

如何查询多对多关系sequelize?

如何在 Sequelize 中按多对多关系排序?

多对多注释未在选择查询中生成连接

续集在关联模型上的位置

hibernate多对多查询

具有额外多对多关系的 JPA 多对多