Node.js Sequelize-如何按关联进行筛选(归属)?

Node.js Sequelize-如何按关联进行筛选(归属)?,node.js,sequelize.js,Node.js,Sequelize.js,我有一个模型,比如说邮政和国家。我想过滤并获取国家id为1或国家id必须为1和2的帖子,如何使用Sequelize实现这一点?我在下面尝试的方式是给我一些不是我期望的东西 非常感谢 Post.js class Post extends Sequelize.Model {} Post.init( { id: { type: Sequelize.INTEGER, primaryKey: true, },

我有一个模型,比如说邮政和国家。我想过滤并获取国家id为1或国家id必须为1和2的帖子,如何使用Sequelize实现这一点?我在下面尝试的方式是给我一些不是我期望的东西

非常感谢

Post.js

class Post extends Sequelize.Model {}

Post.init(
    {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
        },
        text: {
            type: Sequelize.STRING,
        }
    }
);

module.exports = Post;
const Sequelize = require('sequelize');
const sequelize = require('../config/db');

class Country extends Sequelize.Model {}
Country.init({
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    name: {
        type: Sequelize.STRING,
        unique: true
    }
}, {sequelize, modelName: 'country'});

module.exports = Country;
Post.belongsToMany(Country, {through: "PostCountry", foreignKey: 'id'});
Country.js

class Post extends Sequelize.Model {}

Post.init(
    {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
        },
        text: {
            type: Sequelize.STRING,
        }
    }
);

module.exports = Post;
const Sequelize = require('sequelize');
const sequelize = require('../config/db');

class Country extends Sequelize.Model {}
Country.init({
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    name: {
        type: Sequelize.STRING,
        unique: true
    }
}, {sequelize, modelName: 'country'});

module.exports = Country;
Post.belongsToMany(Country, {through: "PostCountry", foreignKey: 'id'});
App.js

class Post extends Sequelize.Model {}

Post.init(
    {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
        },
        text: {
            type: Sequelize.STRING,
        }
    }
);

module.exports = Post;
const Sequelize = require('sequelize');
const sequelize = require('../config/db');

class Country extends Sequelize.Model {}
Country.init({
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    name: {
        type: Sequelize.STRING,
        unique: true
    }
}, {sequelize, modelName: 'country'});

module.exports = Country;
Post.belongsToMany(Country, {through: "PostCountry", foreignKey: 'id'});
我正在尝试的

 Post.findAndCountAll({
        order: [[Country, "name", "asc"]],
        include: [
            {
                model: Country,
                through: { where: { countryId: [2] } }
            }
        ],
        distinct: true,
        offset: offset,
        limit: limit
    })
样本输出

{
    "page": 1,
    "totalPages": 1,
    "totalResults": 1,
    "results": [
        {
            "id": 123,
            "text": "hello, world",
            "countries": [
                {
                    "id": 2,
                    "name": "Australia",
                },
                {
                    "id": 1,
                    "name": "New Zealand",
                },
                {
                    "id": 3,
                    "name": "Singapore",
                }
            ]
        }
    ]
}


一方面,看起来您混淆了
(在本查询中不应该是必需的)和
其中的
(必需的)。试试这个:

Post.findAndCountAll({
        order: [[Country, "name", "asc"]],
        include: [
            {
                model: Country,
                where: { countryId : {[Op.in]: [2]}}
            }
        ],
        distinct: true,
        offset: offset,
        limit: limit
    })

附录----


要显示所有国家,您需要两个联接—一个用于标识某个特定国家的帖子,另一个用于标识其所有关联国家。。。如中所述。FWIW,我还没有在FindCountAll上尝试过,但它应该会起作用…

我可以要求进一步澄清一下吗,现在查询会完美地返回我需要的帖子,但在回复中,对于国家,它只返回我筛选的国家,并删除与帖子相关的所有其他国家。是否有可能在结果中也返回这些国家?再次非常感谢