Repository navigation
Closed
Labels
type: bugDEPRECATED: replace with the "bug" issue type
Description
sequelize is throwing an error when it shouldn't. Below is a simple program to replicate the problem.
I'd expect the program to run without any problems, and give the appropriate findAndCountAll output. The using the same query with then findAll and count functions returns the correct results.
"use strict";
var Sequelize = require('sequelize');
var sequelize = new Sequelize(
'development', // database
'development', // username
'development', // password
{
host: 'localhost',
dialect: 'postgres'
}
);
var Author = sequelize.define('Author', {
firstName: {type: Sequelize.STRING},
lastName: {type: Sequelize.STRING}
});
var Book = sequelize.define('Book', {
title: {type: Sequelize.STRING}
});
var firstAuthor;
var secondAuthor;
Author.hasMany(Book, {constraints: true});
Book.belongsTo(Author, {constraints: true});
var query = {
where: {
'Author.lastName': 'Testerson'
},
include: [Author]
};
sequelize.sync({force: true})
.then(function () {
return Author.create({firstName: 'Test', lastName: 'Testerson'});
})
.then(function (author1) {
firstAuthor = author1;
return Author.create({firstName: 'The Invisible', lastName: 'Hand'});
})
.then(function (author2) {
secondAuthor = author2;
return Book.create({AuthorId: firstAuthor.id, title: 'A simple book'});
})
.then(function () {
return Book.create({AuthorId: firstAuthor.id, title: 'Another book'});
})
.then(function () {
return Book.create({AuthorId: secondAuthor.id, title: 'Some other book'});
})
.then(function () {
// This is the part you're after.
return Book.count(query);
})
.then(function (count) {
console.log('Count result: ' + count);
return Book.findAll(query);
})
.then(function (books) {
console.log('Books result length: ' + books.length);
return Book.findAndCountAll(query);
})
.then(function () {
console.log('There should not be an error');
process.exit(0);
});And it produces the following output:
... <snip>
Executing (default): SELECT count("Book"."id") AS "count" FROM "Books" AS "Book" LEFT OUTER JOIN "Authors" AS "Author" ON "Book"."AuthorId" = "Author"."id" WHERE "Author"."lastName"='Testerson';
Count result: 2
Executing (default): SELECT "Book"."id", "Book"."title", "Book"."createdAt", "Book"."updatedAt", "Book"."AuthorId", "Author"."id" AS "Author.id", "Author"."firstName" AS "Author.firstName", "Author"."lastName" AS "Author.lastName", "Author"."createdAt" AS "Author.createdAt", "Author"."updatedAt" AS "Author.updatedAt" FROM "Books" AS "Book" LEFT OUTER JOIN "Authors" AS "Author" ON "Book"."AuthorId" = "Author"."id" WHERE "Author"."lastName"='Testerson';
Books result length: 2
Executing (default): SELECT count(*) AS "count" FROM "Books" AS "Book" WHERE "Author"."lastName"='Testerson';
Possibly unhandled SequelizeDatabaseError: missing FROM-clause entry for table "Author"
at module.exports.Query.formatError (/snip/to/path/node_modules/sequelize/lib/dialects/postgres/query.js:302:16)
at null.<anonymous> (/snip/to/path/node_modules/sequelize/lib/dialects/postgres/query.js:64:21)
at emit (events.js:95:17)
at Query.handleError (/snip/to/path/node_modules/pg/lib/query.js:99:8)
at null.<anonymous> (/snip/to/path/node_modules/pg/lib/client.js:166:26)
at emit (events.js:95:17)
at Socket.<anonymous> (/snip/to/path/node_modules/pg/lib/connection.js:109:12)
at Socket.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:764:14)
at Socket.emit (events.js:92:17)
From previous event:
... <snip>
I pulled the code from this SO question, although I had to make a bunch of minor changes to get it to work.
I'm still pretty new to this, so I don't know what this sort of error is called or how to classify it. If you have any suggestions for a better title, etc. I'm open.
Metadata
Metadata
Assignees
Labels
type: bugDEPRECATED: replace with the "bug" issue type
Activity
mickhansen commentedon Jan 20, 2015
Includes are not a 100% when it comes to subquery generating code, but we are improving it constantly.
If you could transform your code here into a failing unit test in a PR that would be a great help too.