sequelize: limit option produces 'missing FROM-clause entry for table' error

Issue Description

findOne produces ‘missing FROM-clause entry for table’ error on PostgreSQL.

What are you doing?

User.js

module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
  }, {
    tableName: 'users',
    paranoid: false,
    timestamps: false,
  });
  User.associate = function(models) {
    // associations can be defined here
    User.hasMany(models.Project, {
      foreignKey: {
        name: 'userId',
        allowNull: false,
      },
    });
  };
  return User;
};

Project.js

module.exports = (sequelize, DataTypes) => {
  const Project = sequelize.define('Project', {
    value: {
      type: DataTypes.STRING,
      allowNull: false,
    },
  }, {
    tableName: 'projects',
    paranoid: false,
    timestamps: false,
  });
  Project.associate = function(models) {
    // associations can be defined here
    Project.belongsTo(models.User, {
      foreignKey: {
        name: 'userId',
        allowNull: false,
      },
    });
  };
  return Project;
};

I’d like to find any (findOne) user without projects.

const user = await models.User.findOne({
  where: {
    '$Project.value$': { [models.Sequelize.Op.eq]: null }
  },
  include: [{
    model: models.Project,
    required: false,
  }],
});

What do you expect to happen?

A user without projects.

What is actually happening?

error: missing FROM-clause entry for table "Projects"

Additional context

findAll works as expected.

const users = await models.User.findAll({
  where: {
    '$Project.value$': { [models.Sequelize.Op.eq]: null }
  },
  include: [{
    model: models.Project,
    required: false,
  }],
});

Environment

  • Sequelize version: 5.21.1
  • Node.js version: v10.16.3
  • Operating System: macOS/Docker
  • If TypeScript related: TypeScript version: no

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don’t know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don’t know how to start, I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 9
  • Comments: 29 (7 by maintainers)

Most upvoted comments

const users = await models.User.findAll({
  where: {
    '$Project.value$': { [models.Sequelize.Op.eq]: null }
  },
  include: [{
    model: models.Project,
    required: false,
  }],
  limit: 1000,
});

Produces:

SELECT "User".* FROM (SELECT "User"."id", "User"."kind", "User"."value" FROM "users" AS "User" WHERE "Project"."value" IS NULL LIMIT 1000) AS "User" LEFT OUTER JOIN "projects" AS "Project" ON "User"."id" = "Project"."userId";

And the error: error: missing FROM-clause entry for table "Projects"


const users = await models.User.findAll({
  where: {
    '$Project.value$': { [models.Sequelize.Op.eq]: null }
  },
  include: [{
    model: models.Project,
    required: false,
  }],
  limit: 1000,
  subQuery: false,
});

Produces:

SELECT "User"."id", "User"."kind", "User"."value" FROM "users" AS "User" LEFT OUTER JOIN "projects" AS "Project" ON "User"."id" = "Project"."userId" WHERE "Project"."value" IS NULL LIMIT 1000;

Hi,

subQuery: false, helps me, but it was not obvious.

is this expected to be solved?

Expected:

SELECT "User"."id", "User"."kind", "User"."value" FROM "users" AS "User" LEFT OUTER JOIN "projects" AS "Project" ON "User"."id" = "Project"."userId" WHERE "Project"."value" IS NULL LIMIT 1000;

Actual:

SELECT "User".* FROM (SELECT "User"."id", "User"."kind", "User"."value" FROM "users" AS "User" WHERE "Project"."value" IS NULL LIMIT 1000) AS "User" LEFT OUTER JOIN "projects" AS "Project" ON "User"."id" = "Project"."userId";

Update limit option brakes findAll query like findOne.

Hi @papb limit option still brakes find* queries even in 6.0.0-beta.5 @mahnunchik,

subQuery: false resolved my error: error: missing FROM-clause entry for table ..., but I have the same problem with the limit when i use subQuery: false.

Did anyone resolve this issue?

Yep subQuery: false helps me.

Ok, but the limit stopped working. How can you resolve this issue?

Yep, I’ve got expected output. But I don’t know about performance.