Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list #990

Open
KacperMekarski opened this issue Mar 28, 2022 · 6 comments
Labels

Comments

@KacperMekarski
Copy link

KacperMekarski commented Mar 28, 2022

Rails v6.0.4.6
Ruby v2.5.3

Issue

This statement is invalid with SELECT DISTINCT clause:

PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
LINE 1: ...dly_id_slugs"."slug" = 'product-3-35555' ORDER BY "friendly_...

Solution

Add friendly_id_slugs.id to select statement:

def slug_table_record(id)
  select(quoted_table_name + '.*', Slug.arel_table[:id]).joins(:slugs).where(slug_history_clause(id)).order(Slug.arel_table[:id].desc).first
end
@brunnossanttos
Copy link

Did you find the solution?

@petergoldstein
Copy link
Collaborator

Does anyone have a reproducible test case that we can add to the specs?

@brunnossanttos
Copy link

brunnossanttos commented Jun 24, 2022

I don´t have specs, but I will try explain.

I have a repository method for return promotions paginated and ordered for a logical field(qtdViews)

public async viewsOrdenation(
    { order_by, logged_user_id },
    { page, skip, take }: SearchParams,
  ): Promise<IPaginatorPromotion> {
    const promotions = await this.ormRepository
      .createQueryBuilder('promotions')
      .loadRelationCountAndMap('promotions.qtdComments', 'promotions.comments')
      .loadRelationCountAndMap('promotions.qtdViews', 'promotions.views')
      .loadRelationCountAndMap(
        'promotions.qtdLikes',
        'promotions.likes',
        'il',
        qb => qb.where('il.is_liked = :is_liked', { is_liked: true }),
      )
      .loadRelationCountAndMap(
        'promotions.is_liked',
        'promotions.likes',
        'iL',
        qb =>
          qb
            .where('iL.profile_id = :profile_id', {
              profile_id: logged_user_id,
            })
            .andWhere('iL.is_liked = :is_liked', { is_liked: true }),
      )
      .leftJoinAndSelect('promotions.advertiser', 'advertiser')
      .leftJoin('advertiser.profile', 'profile')
      .addSelect([
        'profile.id',
        'profile.name',
        'profile.email',
        'profile.cellphone',
        'profile.avatar',
      ])
      .leftJoin('promotions.views', 'views')
      .addSelect('COUNT(views.id) as qtdViews')
      .where('promotions.is_expired = :is_expired', { is_expired: false })
      .orderBy('qtdViews', 'DESC')
      .groupBy('promotions.id, profile.id, advertiser.id')
      .skip(skip)
      .take(take)
      .getMany();

    const count = await this.ormRepository
      .createQueryBuilder('promotions')
      .where('promotions.is_expired = :is_expired', { is_expired: false })
      .getCount();

    const data = {
      per_page: take,
      total: count,
      cuurent_page: page,
      data: promotions,
    };

    return data;
  }

    return data;
  }

But when the route it´s called return the error:

"QueryFailedError: for SELECT DISTINCT, ORDER BY expressions must appear in select list"

Can you help me, please?

@parndt
Copy link
Collaborator

parndt commented Jul 21, 2022

@brunnossanttos that's all TypeScript code you posted above, so I'm not quite sure how that relates to this issue 😄

@stale
Copy link

stale bot commented Nov 13, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Nov 13, 2022
@parndt
Copy link
Collaborator

parndt commented Nov 15, 2022

Add friendly_id_slugs.id to select statement:

won't this conflict with the main table's id column?

@stale stale bot removed the stale label Nov 15, 2022
@parndt parndt added the pinned label Nov 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants