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)
Produces:
And the error:
error: missing FROM-clause entry for table "Projects"
Produces:
Hi,
subQuery: false,
helps me, but it was not obvious.is this expected to be solved?
Expected:
Actual:
Update
limit
option brakesfindAll
query likefindOne
.subQuery: false
resolved my error:error: missing FROM-clause entry for table ...
, but I have the same problem with thelimit
when i usesubQuery: false
.Did anyone resolve this issue?
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.