通过多对多关系表检索行的顺序

时间:2019-01-11 15:08:21

标签: node.js sequelize.js

我有两个通过from subprocess import Popen ,PIPE proc = Popen(['python.exe','ex1.py'], stdout=PIPE, stderr=PIPE) out, err = proc.communicate() print(out, err) 关系表链接的模型CompanyContractor

company.js

CompanyContractor

contractor.js

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Company = sequelize.define('Company', {
    name: {
      type: DataTypes.STRING(30),
      allowNull: false,
      unique: true
    },
    slug: {
      type: DataTypes.STRING(30),
      allowNull: false
    },
    description: DataTypes.STRING(200),
  }, {});

  Company.associate = function(models) {
    Company.belongsToMany(models.Contractor, { through: 'CompanyContractor', as: 'contractors',foreignKey: 'companyId' });
  };

  return Company;
};

companyContractor.js

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Contractor = sequelize.define('Contractor', {
    name: {
      type: DataTypes.STRING(50),
      allowNull: false
    }
  }, {});
  Contractor.associate = function(models) {
    Contractor.belongsToMany(models.Company, { through: 'CompanyContractor', as: 'contractors' });
  };
  return Contractor;
};

是否可以通过'use strict'; module.exports = (sequelize, DataTypes) => { const CompanyContractor = sequelize.define('CompanyContractor', { companyId: { type: DataTypes.INTEGER, allowNull: false }, contractorId: { type: DataTypes.INTEGER, allowNull: false } }, { timestamps: false }); return CompanyContractor; }; Contractor.findAll()

1 个答案:

答案 0 :(得分:0)

Contractor.findAll({
include: [{
   model: Company,
   through: {
        attributes: ['companyId']
      }
   }]
});

From the documentation