找到所有多对多的

时间:2018-12-20 11:17:28

标签: node.js orm sequelize.js backend

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

选择p.id,pl.id

从人们内部进入人们列表

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上出现,但我什至没有使用。

怎么了?

1 个答案:

答案 0 :(得分:0)

这就是我的使用方式。

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
  })

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

ArtistGenre模型:

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'});