Repository navigation
Closed
Labels
Description
Hello, thanks for sequelize, but I stuck with problem:
Here's my code:
models.Product.belongsToMany(models.Category, through: 'CategoriesProducts')
models.Category.belongsToMany(models.Product, through: 'CategoriesProducts')
...
db.Product.findAll(
order: 'Product.id asc'
limit: params.limit
offset: params.offset
where:
merchantId: params.merchants,
price:
gte: params.price.min
lte: params.price.max
include:
model: db.Category
where:
id: params.categories
).then (items) ->
...and it's build next query:
SELECT `Product`.*,
`categories`.`id` AS `Categories.id`,
...
`Categories.CategoriesProducts`.`createdat` AS
`Categories.CategoriesProducts.createdAt`,
...
FROM (SELECT `Product`.`id`,
`Product`.`image`,
...
FROM `products` AS `Product`
WHERE `Product`.`merchantid` IN ( '1', '2' )
AND `Product`.`price` >= '0'
AND `Product`.`price` <= '10000'
AND (SELECT `categoriesproducts`.`productid`
FROM `categoriesproducts` AS `CategoriesProducts`
INNER JOIN `categories` AS `Category`
ON `categoriesproducts`.`categoryid` =
`Category`.`id`
WHERE `Product`.`id` = `categoriesproducts`.`productid`
LIMIT 1) IS NOT NULL
LIMIT 0, 12) AS `Product`
INNER JOIN `categoriesproducts` AS `Categories.CategoriesProducts`
ON `Product`.`id` = `Categories.CategoriesProducts`.`productid`
INNER JOIN `categories` AS `Categories`
ON `categories`.`id` =
`Categories.CategoriesProducts`.`categoryid`
AND `categories`.`id` IN ( '1' )
ORDER BY product.id ASC; it's mean that I often get less than 12 rows, but I expect something like that:
SELECT `Product`.`id`,
...
`categories`.`id` AS `Categories.id`,
...
`Categories.CategoriesProducts`.`createdat` AS `Categories.CategoriesProducts.createdAt`,
...
FROM `products` AS `Product`
INNER JOIN `categoriesproducts` AS `Categories.CategoriesProducts`
ON `Product`.`id` = `Categories.CategoriesProducts`.`productid`
INNER JOIN `categories` AS `Categories`
ON `categories`.`id` = `Categories.CategoriesProducts`.`categoryid`
AND `categories`.`id` IN ( '1' )
WHERE `Product`.`merchantid` IN ( '1', '2' )
AND `Product`.`price` >= '0'
AND `Product`.`price` <= '10000'
ORDER BY product.id ASC
LIMIT 12 OFFSET 0; I get this built query just by deleting
limit: params.limit
offset: params.offset
from parameters object and write LIMIT 12 OFFSET 0; manually.
Question: how to get what I expect (returning 12 rows) or how to get built query for manually changing it?
Activity
mickhansen commentedon Jan 10, 2015
Your expected query wouldn't ever actually work in terms of limit because of row duplication.
As for the sequelize calls, it appears it's not accurately using the include tables in the subquery for checking (
categories.id IN ( '1' )should be checked but it isnt)What version are you using? Have you tested on the latest?
milushov commentedon Jan 10, 2015
my version is
Sequelize [Node: 0.10.26, CLI: 1.0.2, ORM: 2.0.0-rc7, sqlite3: ^3.0.4]mickhansen commentedon Jan 10, 2015
Okay, so appears you uncovered yet a case :)
If you want to help out we would appreciate a PR with a failing unit test.
milushov commentedon Jan 10, 2015
ok, I will try in the near future
hmm, btw, where is proper place for this test case?
mickhansen commentedon Jan 10, 2015
test/include/findAllhas a ton of other cases so that's fine :)milushov commentedon Jan 13, 2015
@mickhansen, hey, can you prompt me where is placed migration files for these models? I want to create new join table for
belongToManyassociation.mickhansen commentedon Jan 13, 2015
@milushov sorry? We don't use migrations for general testing - We use sync()
milushov commentedon Jan 13, 2015
@mickhansen, ok, so how and where I can create join table without defining model?
mickhansen commentedon Jan 13, 2015
@milushov If it doesn't need to hold more than the foreign key attributes you just use
through: 'tableName'on your BTM relations.BridgeAR commentedon May 31, 2015
Ping @milushov
NOX73 commentedon Sep 9, 2015
Is anybody doing anything about this? btw counting in this case works properly
janmeier commentedon Sep 9, 2015
@NOX73 Can you describe the issue you are having? Model definition, associations etc? It's always easier to figure out the bug with more test cases
abossard commentedon Oct 14, 2015
I have the same problem. I have these models:
And my query looks like this:
I get the current count (e.g. 42), but the rows have only 6 in them.
abossard commentedon Oct 14, 2015
If I raise the limit from 68 to e.g. 999 I get all the 42 results! The sweet spot is around limit 661, from there I get all the rows.
Here is the full SQL (I omitted some of the classes before):
abossard commentedon Oct 15, 2015
Clearly it's not possible to use LIMIT with this SQL. It would only work, if everything could be loaded separate.
mickhansen commentedon Oct 15, 2015
@abossard We actually have
include.separatefor hasMany, belongsToMany support is pending.Limits do become tricky with large includes.
abossard commentedon Oct 15, 2015
I'd be happy to ditch all includes and have them all in separate queries, if that would make LIMIT work better. Funny thing is, if there'd be a count query, that returns the IDs instead of the counts, these could be used to make the huge query with includes without regarding the limits.
abossard commentedon Oct 15, 2015
well, it would not regard the sorting. I see. It is a tricky problem.
abossard commentedon Oct 15, 2015
Currently the LIMIT is on the inner table, but the WHERE clause is on the outer table. That limits the products before they even get to the matching with the WHERE clause.
I wish I had the skills and time to supply you with a test and make a pull request :-/
BridgeAR commentedon Jun 30, 2016
@abossard to reach what you want you currently would have to write something like:
But this is not yet working, as the ProductColor would be removed in the count query and produce a database error. I'm about to fix this though and look through all issues that correspond to 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 🙂