loading...

Sequelize Association Logic (Help)

tahsinature profile image Mohammad Tahsin ・1 min read

I want to exclude a user if my selected number is in its limited range. It's working if there is one row (limitation row). If there are multiple rows, Sequelize is selecting the user. How to tell Sequelize not to select that record?

[
  {
    "id": 2,
    "name": "Gussie Spinka IV",
    "limitations": [
      {
        "id": 3,
        "start": 50,
        "end": 70,
        "userId": 2
      }
    ]
  },
  {
    "id": 1,
    "name": "Francesco Rutherford",
    "limitations": [
      {
        "id": 2,
        "start": 5,
        "end": 10,
        "userId": 1
      },
      {
        "id": 1,
        "start": 20,
        "end": 40,
        "userId": 1
      }
    ]
  },
  {
    "id": 10,
    "name": "Tressa Walter",
    "limitations": []
  },
  {
    "id": 5,
    "name": "Skyla Lubowitz",
    "limitations": []
  },
  {
    "id": 8,
    "name": "Taryn Will",
    "limitations": []
  },
  {
    "id": 6,
    "name": "Vito Hilpert",
    "limitations": []
  },
  {
    "id": 4,
    "name": "Miss Anais Runolfsdottir",
    "limitations": []
  },
  {
    "id": 3,
    "name": "Adell O'Hara",
    "limitations": []
  },
  {
    "id": 9,
    "name": "Mr. Verda Murphy",
    "limitations": []
  },
  {
    "id": 7,
    "name": "Brooklyn Stamm",
    "limitations": []
  }
]

router.get('/', async function(req, res, next) {
  const dn = 30;
  const users = await User.findAll({
    include: {
      model: Limitation,
      where: Sequelize.literal(`'${dn}' NOT BETWEEN "start" AND "end"`),
    },
  });
  res.send(users);
});


Posted on by:

Discussion

pic
Editor guide