Sequelize missing FROM-clause entry for table Postgres

Viewed 6518

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'],
    ],
  }
5 Answers

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

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 }

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.

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,
        },
    ],
},

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'],
    ],
  }
Related