16

I am trying to filter my DataRow objects by where the included DataPoints data is loc and sortOrder is 2. Below is the query I am trying. I keep getting the following error.

SequelizeDatabaseError: missing FROM-clause entry for table "dataPoints"

I have tried setting required: true and duplicating: false with no luck. I tried these in both the DataRows include as well as DataPoints.

    attributes: ['id', 'name', 'labId'],
    include: [{
      as: 'dataColumns',
      model: DataColumn,
      attributes: ['id', 'name', 'sortOrder', 'dataType', 'isDate', 'isLocation'],
    }, {
      as: 'dataSets',
      model: Study,
      attributes: ['id', 'name'],
    }, {
      as: 'dataRows',
      model: DataRow,
      attributes: ['id'],
      where: {
        [Op.and]: [
          {
            '$dataPoints.data$': 'loc',
          },
          {
            '$dataPoints.sortOrder$': 2,
          },
        ],
      },
      include: [{
        as: 'dataPoints',
        model: DataPoint,
        attributes: ['id', 'sortOrder', 'data'],
      }],
    },],
    order: [
      [{ model: DataColumn, as: 'dataColumns' }, 'sortOrder', 'ASC'],
      [{ model: DataRow, as: 'dataRows' }, { model: DataPoint, as: 'dataPoints' }, 'sortOrder', 'ASC'],
    ],
  }
1
  • Did you find any solution? Commented Apr 9, 2021 at 17:26

5 Answers 5

16

Try required: true and duplicating: false on all the models that you're trying to include. This would solve the problem. Worked for me.

Sign up to request clarification or add additional context in comments.

2 Comments

Worked nice here! Can you explain why this properties work?
if we need left join then how we can do required true
9

If you are here like me who has used a where condition on a column of included table in the primary where and got this error, just use this in the options of primary query.

{ subQuery: false }

2 Comments

Here's a Sequelize docs on that: sequelize.org/docs/v6/other-topics/sub-queries Unfortunately it does not explain the case with subQuery when using this kind of "Primary where" search with included models.
This breaks limit for me sadly
1

Follow this code:

attributes: ['id', 'name', 'labId'],
    include: [{
      as: 'dataColumns',
      model: DataColumn,
      attributes: ['id', 'name', 'sortOrder', 'dataType', 'isDate', 'isLocation'],
    }, {
      as: 'dataSets',
      model: Study,
      attributes: ['id', 'name'],
    }, {
      as: 'dataRows',
      model: DataRow,
      attributes: ['id'],
      where: {
        [Op.and]: [
          {
            '$dataPoints.data$': 'loc',
          },
          {
            '$dataPoints.sortOrder$': 2,
          },
        ],
      },
      include: [{
        as: 'dataPoints',
        model: DataPoint,
        attributes: ['id', 'sortOrder', 'data'],
      }],
    },],
    order: [
      [{ model: model.DataColumn, as: 'dataColumns' }, 'sortOrder', 'ASC'],
      [{ model: model.DataRow, as: 'dataRows' }, { model: DataPoint, as: 'dataPoints' }, 'sortOrder', 'ASC'],
    ],
  }

1 Comment

Looks good approach, though I have not tested myself yet.
0

Required and duplicating set to true and false worked , the issue comes when you try to eager load a model and the search field is not unique, in my case i was able to set my field as unique, getting rid of setting required and duplicating properties in the eager load query.

Comments

0

You need to move this code

where: {
    [Op.and]: [
        {
            "$dataPoints.data$": "loc",
        },
        {
            "$dataPoints.sortOrder$": 2,
        },
    ],
},

to up below attributes and change to

where: {
    [Op.and]: [
        {
            "$dataRows.dataPoints.data$": "%loc%",
        },
        {
            "$dataRows.dataPoints.sortOrder$": 2,
        },
    ],
},

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.