question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:26 (7 by maintainers)

github_iconTop GitHub Comments

18reactions
mahnunchikcommented, Nov 11, 2019
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;
12reactions
mahnunchikcommented, Nov 1, 2019

Hi,

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fix “ERROR: missing FROM-clause entry for table” in ...
Another way to fix it is to use an alias for the column: (SELECT TeacherName t FROM Teachers) UNION (SELECT StudentName FROM Students)...
Read more >
PG::UndefinedTable: ERROR: missing FROM-clause entry for ...
I am trying to get a list of uniq patients who have submitted an esas_assessment , sorted by the time the esas_assessment was...
Read more >
Missing FROM-clause entry for table in postgres CTE
You are facing with SQL Error [42P01]: ERROR: missing FROM-clause entry for table "a_cte" error because you never select from a_cte .
Read more >
PostgreSQL : Documentation: 9.6: SELECT
If the LIMIT (or FETCH FIRST ) or OFFSET clause is specified, the SELECT ... If RETURNING is omitted, the statement is still...
Read more >
Re: " Adding missing FROM-clause entry for table .... " problem.
> "allow_implicit_from", with options "ok" (completely permissive),. > "notice" (the current behavior), and "error" (disallow it). That's a workable compromise ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found