Skip to content

Include with where and order on aliased column creates incorrect query #8100

Open

Description

@kingjerod

What you are doing?

Getting latest row by teamId, includes an association, and sorting on a column created_at with an alias of createdAt.

const Order = sequelize.define('orders', {
  id: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
  customerId: {type: Sequelize.INTEGER, field: "customer_id"},
  product: {type: Sequelize.STRING},
}, {timestamps: false});
const Customer = sequelize.define('customers', {
  id: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
  teamId: {type: Sequelize.INTEGER, field: "team_id"},
  createdAt: {type: Sequelize.DATEONLY, field: "created_at"},
}, {timestamps: false});
Customer.hasMany(Order, {foreignKey: "customerId"});
const customer = await Customer.findOne({where: {teamId: 5}, include: [Order], order: [["createdAt"]]});

What do you expect to happen?

I expect it to generate a query to get the latest customer with team_id = 5, including all their orders.

What is actually happening?

Sequelize is generating an invalid query. The inner query aliased the created_at column to createdAt correctly, so it does not exist in the outer query to order by. This causes the error column customers.created_at does not exist.

SELECT
	"customers".*, "orders"."id" AS "orders.id",
	"orders"."customer_id" AS "orders.customerId",
	"orders"."product" AS "orders.product"
FROM
	(
		SELECT
			"customers"."id",
			"customers"."team_id" AS "teamId",
			"customers"."created_at" AS "createdAt"
		FROM
			"customers" AS "customers"
		WHERE
			"customers"."team_id" = 5
		ORDER BY
			"customers"."created_at"
		LIMIT 1
	) AS "customers"
LEFT OUTER JOIN "orders" AS "orders" ON "customers"."id" = "orders"."customer_id"
ORDER BY
	"customers"."created_at";

Dialect: postgres
Database version: 9.6.2
Sequelize version: 4.4.3

Activity

  1. Chapurin commented on Aug 12, 2017

    @Chapurin

    Sequelize ORDER in subquery and main query this problem for me to

  2. Chapurin commented on Aug 12, 2017

    @Chapurin

    The solution for me is to roll back to version v3.24.3 and use order as plain string (not as an array value) ((

  3. gc1508 commented on Aug 14, 2017

    @gc1508

    GROUP BY also causes issues as far as I can tell.

  4. hiradimir commented on Sep 16, 2017

    @hiradimir

    I have same issue.
    I can not find workaround

  5. stale commented on Dec 15, 2017

    @stale

    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 🙂

  6. kingjerod commented on Dec 15, 2017

    @kingjerod
    Author

    This is still an issue.

  7. aprilmintacpineda commented on Feb 23, 2018

    @aprilmintacpineda
  8. jharting commented on Mar 15, 2018

    @jharting
    Contributor

    @kingjerod this should be fixed with #9188
    Can you verify?

  9. stale commented on Jun 13, 2018

    @stale

    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 🙂

  10. faq885 commented on Mar 31, 2019

    @faq885

    i am still having issues with this...

  11. Exilz commented on Jan 12, 2020

    @Exilz

    I still cannot fix this problem unless I set subQuery: false in the fetchAll method options.

  12. reopened this on Jan 19, 2020
  13. added
    existing workaround
    type: bug
    on Jan 19, 2020
  14. aristov commented on Mar 24, 2020

    @aristov
    Contributor

    subQuery: false helps, but breaks the limit/offset.

  15. added theissue type on Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    existing workaroundtype: bug

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions