mysql Sequelize findAll受包含模型上的位置影响

zzoitvuj  于 2023-05-28  发布在  Mysql
关注(0)|答案(1)|浏览(159)

我有2个模型要求和要求投票。当返回询问和包括asksVote我想只返回当前用户asksVote但所有的询问记录。当前,include中的where影响整个数据集。如果我能知道搜索结果的话这种行为就说得通了。任何帮助将不胜感激。谢谢你!
ask.js:

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Ask extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
      Ask.belongsTo(models.user);
      Ask.hasMany(models.asksVote);

    }
  };
  Ask.init({
    title: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    discription: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    type: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    implemented: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    upvotes: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: +1
    },

  }, {
    sequelize,
    modelName: 'ask',
    paranoid: true
  });
  return Ask;
};

asksVote.js:

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class AsksVote extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
      AsksVote.belongsTo(models.user);
      AsksVote.belongsTo(models.ask);

    }
  };
  AsksVote.init({
    upvote: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true
    },
  }, {
    sequelize,
    modelName: 'asksVote',
    paranoid: true
  });
  return AsksVote;
};

查询代码:

const userId = req.params.userId;
    Ask.findAll({
        limit: 10,
        order: [
            ['createdAt', 'DESC'],
        ],
        include: {
            model: AsksVote,
            where: {
                userId: userId
            }
        }
    })
    .then( response => {
        return res.status(200).json(response);
    })
    .catch( err => {
        return res.status(500).json(err);
    });

当前结果:

{
        "id": 7,
        "title": "ajkfsldkfa",
        "discription": "lkjfsdlkfgjdfslkasdf",
        "type": "niche",
        "implemented": false,
        "upvotes": 1,
        "createdAt": "2023-05-22T19:09:56.000Z",
        "updatedAt": "2023-05-22T19:09:56.000Z",
        "deletedAt": null,
        "userId": 1,
        "asksVotes": [
            {
                "id": 6,
                "upvote": true,
                "createdAt": "2023-05-22T19:09:56.000Z",
                "updatedAt": "2023-05-22T19:09:56.000Z",
                "deletedAt": null,
                "askId": 7,
                "userId": 1
            }
        ]
    },
    {
        "id": 6,
        "title": "trselkdfjasd as dfsjknfsad fsdfkjlasdf sadf s df dfgs xvc ",
        "discription": "fjgklsdjgbd  asdv  sdv sd vsd vsd",
        "type": "question",
        "implemented": false,
        "upvotes": 1,
        "createdAt": "2023-05-22T19:09:19.000Z",
        "updatedAt": "2023-05-22T19:09:19.000Z",
        "deletedAt": null,
        "userId": 1,
        "asksVotes": [
            {
                "id": 5,
                "upvote": true,
                "createdAt": "2023-05-22T19:09:19.000Z",
                "updatedAt": "2023-05-22T19:09:19.000Z",
                "deletedAt": null,
                "askId": 6,
                "userId": 1
            }
        ]
    },
    {
        "id": 5,
        "title": "jdfsalk;dfjasd",
        "discription": "sdfasdfasdfvzxvcsdarfgasdf",
        "type": "idea",
        "implemented": false,
        "upvotes": 1,
        "createdAt": "2023-05-22T19:07:11.000Z",
        "updatedAt": "2023-05-22T19:07:11.000Z",
        "deletedAt": null,
        "userId": 1,
        "asksVotes": [
            {
                "id": 4,
                "upvote": true,
                "createdAt": "2023-05-22T19:07:11.000Z",
                "updatedAt": "2023-05-22T19:07:11.000Z",
                "deletedAt": null,
                "askId": 5,
                "userId": 1
            }
        ]
    },
    {
        "id": 4,
        "title": "dasd",
        "discription": "fsadfdsafsd",
        "type": "question",
        "implemented": false,
        "upvotes": 1,
        "createdAt": "2023-05-22T19:03:02.000Z",
        "updatedAt": "2023-05-22T19:03:02.000Z",
        "deletedAt": null,
        "userId": 1,
        "asksVotes": [
            {
                "id": 3,
                "upvote": true,
                "createdAt": "2023-05-22T19:03:02.000Z",
                "updatedAt": "2023-05-22T19:03:02.000Z",
                "deletedAt": null,
                "askId": 4,
                "userId": 1
            }
        ]
    }

预期/期望结果:

{
        "id": 8,
        "title": "testing",
        "discription": "testing this out",
        "type": "idea",
        "implemented": false,
        "upvotes": 1,
        "createdAt": "2023-05-22T19:56:57.000Z",
        "updatedAt": "2023-05-22T19:56:57.000Z",
        "deletedAt": null,
        "userId": 33,
        "asksVotes": []
},
{
        "id": 7,
        "title": "ajkfsldkfa",
        "discription": "lkjfsdlkfgjdfslkasdf",
        "type": "niche",
        "implemented": false,
        "upvotes": 1,
        "createdAt": "2023-05-22T19:09:56.000Z",
        "updatedAt": "2023-05-22T19:09:56.000Z",
        "deletedAt": null,
        "userId": 1,
        "asksVotes": [
            {
                "id": 6,
                "upvote": true,
                "createdAt": "2023-05-22T19:09:56.000Z",
                "updatedAt": "2023-05-22T19:09:56.000Z",
                "deletedAt": null,
                "askId": 7,
                "userId": 1
            }
        ]
    },
    {
        "id": 6,
        "title": "trselkdfjasd as dfsjknfsad fsdfkjlasdf sadf s df dfgs xvc ",
        "discription": "fjgklsdjgbd  asdv  sdv sd vsd vsd",
        "type": "question",
        "implemented": false,
        "upvotes": 1,
        "createdAt": "2023-05-22T19:09:19.000Z",
        "updatedAt": "2023-05-22T19:09:19.000Z",
        "deletedAt": null,
        "userId": 1,
        "asksVotes": [
            {
                "id": 5,
                "upvote": true,
                "createdAt": "2023-05-22T19:09:19.000Z",
                "updatedAt": "2023-05-22T19:09:19.000Z",
                "deletedAt": null,
                "askId": 6,
                "userId": 1
            }
        ]
    },
    {
        "id": 5,
        "title": "jdfsalk;dfjasd",
        "discription": "sdfasdfasdfvzxvcsdarfgasdf",
        "type": "idea",
        "implemented": false,
        "upvotes": 1,
        "createdAt": "2023-05-22T19:07:11.000Z",
        "updatedAt": "2023-05-22T19:07:11.000Z",
        "deletedAt": null,
        "userId": 1,
        "asksVotes": [
            {
                "id": 4,
                "upvote": true,
                "createdAt": "2023-05-22T19:07:11.000Z",
                "updatedAt": "2023-05-22T19:07:11.000Z",
                "deletedAt": null,
                "askId": 5,
                "userId": 1
            }
        ]
    },
    {
        "id": 4,
        "title": "dasd",
        "discription": "fsadfdsafsd",
        "type": "question",
        "implemented": false,
        "upvotes": 1,
        "createdAt": "2023-05-22T19:03:02.000Z",
        "updatedAt": "2023-05-22T19:03:02.000Z",
        "deletedAt": null,
        "userId": 1,
        "asksVotes": [
            {
                "id": 3,
                "upvote": true,
                "createdAt": "2023-05-22T19:03:02.000Z",
                "updatedAt": "2023-05-22T19:03:02.000Z",
                "deletedAt": null,
                "askId": 4,
                "userId": 1
            }
        ]
    }
gudnpqoy

gudnpqoy1#

Ask.findAll({
    limit: 10,
    order: [
        ['createdAt', 'DESC'],
    ],
    include: {
        model: AsksVote,
        as: 'asksVotes',
        where: {
            userId: userId
        },
        required: false
    }
})

试试这个代码。通过设置所需:false,Sequelize将执行LEFT OUTER JOIN而不是INNER JOIN,这将返回所有的Ask记录,即使当前用户没有匹配的AsksVote记录。

相关问题