You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
limit causes crashes with include and sequelize.literal #4200
'use strict';
var Sequelize = require('sequelize');
var _ = require('lodash');
var sequelize = new Sequelize('db_name', 'db_user', 'db_pass', {
dialect: 'sqlite',
storage: 'db.sql',
logging: true
});
var User = sequelize.define('User', {
username: Sequelize.STRING,
birthday: Sequelize.DATE
});
var Project = sequelize.define('Project', {
title: Sequelize.STRING,
description: Sequelize.TEXT
});
User.belongsToMany(Project, { through: 'UsersProjects' });
Project.belongsToMany(User, { through: 'UsersProjects' });
var u1 = null;
var query = {
where: {$and: [
sequelize.literal('Projects.id IS NOT null')
]},
include: [
{model: Project, required:true}
]
};
sequelize.sync()
.then(function () {
return User.create({username:'John Snow'});
}).then(function () {
return User.create({username:'Swnow White'});
}).then(function () {
return User.create({username:'IceT'});
}).then(function (user) {
u1 = user;
return Project.create({title:'Project 1'});
}).then(function () {
return Project.create({title:'Project 2'});
}).then(function () {
return Project.create({title:'3rd project'});
}).then( function (project) {
return project.addUser(u1);
}).then( function () {
console.log('User.findAll(query)');
return User.findAll(query);
}).then( function () {
console.log('User.findAll(limitedQuery)');
var limitedQuery = _.clone(query);
limitedQuery.limit = 2;
return User.findAll(limitedQuery);
});
Notice how User.findAll(query); runs correctly while return User.findAll(limitedQuery); crashes:
Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such column: Projects.id
at Query.formatError (.../node_modules/sequelize/lib/dialects/sqlite/query.js:242:14)
at .../node_modules/sequelize/lib/dialects/sqlite/query.js:47:29
at replacement (.../node_modules/sqlite3/lib/trace.js:20:31)
at Statement.errBack (.../node_modules/sqlite3/lib/sqlite3.js:16:21)
SELECT `User`.`id`,
`User`.`username`,
`User`.`birthday`,
`User`.`createdat`,
`User`.`updatedat`,
`projects`.`id` AS `Projects.id`,
`projects`.`title` AS `Projects.title`,
`projects`.`description` AS `Projects.description`,
`projects`.`createdat` AS `Projects.createdAt`,
`projects`.`updatedat` AS `Projects.updatedAt`,
`Projects.UsersProjects`.`createdat` AS
`Projects.UsersProjects.createdAt`,
`Projects.UsersProjects`.`updatedat` AS
`Projects.UsersProjects.updatedAt`,
`Projects.UsersProjects`.`projectid` AS
`Projects.UsersProjects.ProjectId`,
`Projects.UsersProjects`.`userid` AS `Projects.UsersProjects.UserId`
FROM `users` AS `User`
INNER JOIN `usersprojects` AS `Projects.UsersProjects`
ON `User`.`id` = `Projects.UsersProjects`.`userid`
INNER JOIN `projects` AS `Projects`
ON `projects`.`id` = `Projects.UsersProjects`.`projectid`
WHERE ( projects.id IS NOT NULL );
In the second case:
SELECT `User`.*,
`projects`.`id` AS `Projects.id`,
`projects`.`title` AS `Projects.title`,
`projects`.`description` AS `Projects.description`,
`projects`.`createdat` AS `Projects.createdAt`,
`projects`.`updatedat` AS `Projects.updatedAt`,
`Projects.UsersProjects`.`createdat` AS
`Projects.UsersProjects.createdAt`,
`Projects.UsersProjects`.`updatedat` AS
`Projects.UsersProjects.updatedAt`,
`Projects.UsersProjects`.`projectid` AS
`Projects.UsersProjects.ProjectId`,
`Projects.UsersProjects`.`userid` AS `Projects.UsersProjects.UserId`
FROM (SELECT `User`.`id`,
`User`.`username`,
`User`.`birthday`,
`User`.`createdat`,
`User`.`updatedat`
FROM `users` AS `User`
WHERE ( projects.id IS NOT NULL )
LIMIT 2) AS `User`
INNER JOIN `usersprojects` AS `Projects.UsersProjects`
ON `User`.`id` = `Projects.UsersProjects`.`userid`
INNER JOIN `projects` AS `Projects`
ON `projects`.`id` = `Projects.UsersProjects`.`projectid`;
Why not simply wrap the first query in SELECT * FROM(...) LIMIT 2 ?
Resulting in:
SELECT *
FROM (SELECT `User`.`id`,
`User`.`username`,
`User`.`birthday`,
`User`.`createdat`,
`User`.`updatedat`,
`projects`.`id` AS `Projects.id`,
`projects`.`title` AS `Projects.title`,
`projects`.`description` AS `Projects.description`,
`projects`.`createdat` AS `Projects.createdAt`,
`projects`.`updatedat` AS `Projects.updatedAt`,
`Projects.UsersProjects`.`createdat` AS
`Projects.UsersProjects.createdAt`,
`Projects.UsersProjects`.`updatedat` AS
`Projects.UsersProjects.updatedAt`,
`Projects.UsersProjects`.`projectid` AS
`Projects.UsersProjects.ProjectId`,
`Projects.UsersProjects`.`userid` AS
`Projects.UsersProjects.UserId`
FROM `users` AS `User`
INNER JOIN `usersprojects` AS `Projects.UsersProjects`
ON `User`.`id` = `Projects.UsersProjects`.`userid`
INNER JOIN `projects` AS `Projects`
ON `projects`.`id` = `Projects.UsersProjects`.`projectid`
WHERE ( projects.id IS NOT NULL ))
LIMIT 2;
@slawo We already explained why twice in the other issue. Try running your own suggestion though, see if it works :)
For everyone else:
If 2 users have 5 projects each you get 10 rows, LIMIT 2 will then remove 8 of those rows and you might have 1 user with 2 projects instead of what you wanted.
@mickhansen@janmeier You are absolutely right. So far I have a situation where only one dependant object is associated with the main one and this worked. it will not when users start associating more objects together.
@slawo Your issue is definitely something we have to consider when implementing #3095. If there is a clause in the main where that pertains to an include, that query should be removed in cases where we have to remove the include.
But right now, I doubt there is a good fix for this.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
Activity
[-]limit causes crashes with sequelize.literal[/-][+]limit causes crashes with include and sequelize.literal[/+]slawo commentedon Jul 28, 2015
Query generated in the 1st case:
In the second case:
Why not simply wrap the first query in
SELECT * FROM(...) LIMIT 2?Resulting in:
mickhansen commentedon Jul 28, 2015
@slawo We already explained why twice in the other issue. Try running your own suggestion though, see if it works :)
For everyone else:
If 2 users have 5 projects each you get 10 rows, LIMIT 2 will then remove 8 of those rows and you might have 1 user with 2 projects instead of what you wanted.
janmeier commentedon Jul 28, 2015
See this fiddle http://sqlfiddle.com/#!9/d85c0/2 - it returns userid 1 twice, with limit 2
slawo commentedon Jul 28, 2015
@mickhansen @janmeier You are absolutely right. So far I have a situation where only one dependant object is associated with the main one and this worked. it will not when users start associating more objects together.
janmeier commentedon Jul 28, 2015
@slawo Your issue is definitely something we have to consider when implementing #3095. If there is a clause in the main
wherethat pertains to an include, that query should be removed in cases where we have to remove the include.But right now, I doubt there is a good fix for this.
stale commentedon Jun 29, 2017
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂