Skip to content

Search and sort by localized field does not work: Error: Alias "XXX_locales" is already used in this query #7015

Closed

Description

@lazidoca

Link to reproduction

No response

Payload Version

3.0.0-beta.57

Node Version

20 lts

Next.js Version

15 rc 0

Describe the Bug

I have a posts collection with a localized title field. In the Admin panel, I want to filter by the title. It works if the sortby is unset (it will sort by posts.id I think). If I sort by the title, the error occurs:

Error: Alias "posts_locales" is already used in this query

Reproduction Steps

  1. Create a posts collection with a localized title field
  2. go to /admin/collections/posts
  3. create a post record
  4. enter to the search text input to filter by "title"
  5. Sort by title

Adapters and Plugins

postgresAdapter using node-postgres driver

Activity

  1. added
    status: needs-triage
    on Jul 3, 2024
  2. changed the title [-]Search and sort by localized field does not work[/-] [+]Search and sort by localized field does not work: Error: Alias "XXX_locales" is already used in this query[/+] on Jul 3, 2024
  3. lazidoca commented on Jul 3, 2024

    @lazidoca
    Author

    It seems like when there are multiple joins on the the same table, the chainedMethods is pushed with a new left join which causes the issue:

    https://github.com/payloadcms/payload/blob/93bdc0e98d5541fddfe5a143e5678164692d3d37/packages/db-postgres/src/queries/selectDistinct.ts#L37C1-L43C1

  4. lazidoca commented on Jul 3, 2024

    @lazidoca
    Author

    Payload 3.0 beta 56 works fine, so I think the recent changes in beta 57 causes the issue

  5. added
    db: postgres
    on Jul 8, 2024
  6. removed
    status: needs-triage
    on Jul 29, 2024
  7. rainer-friederich commented on Aug 16, 2024

    @rainer-friederich

    Just wanted to add here that this is still an issue. We have this on the image collection when we search there.

    In our image collection the alt and caption fields are localized and the search field then yields an error:

    admin/collections/image?limit=10&page=1&search=test

    Yields

    Bildschirmfoto 2024-08-16 um 10 20 00

    This is because we have the alt and caption in the listSearchableFields:

    fields: [
        {
          name: 'alt',
          label: 'Alternative Text',
          localized: true,
          required: true,
          type: 'text',
          admin: {
            description: 'Alternative text for the image',
          },
        },
        {
          name: 'caption',
          label: 'Caption',
          localized: true,
          type: 'textarea',
          admin: {
            description: 'Description of the image',
          },
        },
      ...
     admin: {
        description: 'Collection used to store image files',
        useAsTitle: 'alt',
        defaultColumns: ['filename', 'alt', 'caption'],
        listSearchableFields: ['filename', 'alt', 'caption'],
      },
    

    If i remove alt and caption from there it works but only searches in the filename.

    Update: This also works if i only remove the caption from the listSearchableFields. So the "alt" somehow works.

  8. tiago-meireles commented on Aug 30, 2024

    @tiago-meireles

    I'm also having this issue.
    This is the error message that appears

          |  GET /admin/collections/pages?limit=10&sort=title 200 in 64ms
          |  ⨯ node_modules/@payloadcms/db-postgres/node_modules/drizzle-orm/pg-core/query-builders/select.js (94:1) @ PgSelectBase.eval [as leftJoin]
          |  ⨯ Error: Alias "_pages_v_locales" is already used in this query
          |     at Array.reduce (<anonymous>)
          | digest: "3529223665"
          |   92 |       const tableName = getTableLikeName(table);
          |   93 |       if (typeof tableName === "string" && this.config.joins?.some((join) => join.alias === tableName)) {
          | > 94 |         throw new Error(`Alias "${tableName}" is already used in this query`);
          |      | ^
          |   95 |       }
          |   96 |       if (!this.isPartialSelect) {
          |   97 |         if (Object.keys(this.joinsNotNullableMap).length === 1 && typeof baseTableName === "string") {
    
  9. r1tsuu commented on Sep 24, 2024

    @r1tsuu
    Member

    Fixed in #8396

  10. github-actions commented on Sep 26, 2024

    @github-actions
    Contributor

    This issue has been automatically locked.
    Please open a new issue if this issue persists with any additional detail.

  11. locked as resolved and limited conversation to collaborators on Sep 26, 2024
  12. github-actions commented on Sep 26, 2024

    @github-actions
    Contributor

    🚀 This is included in version v3.0.0-beta.109

  13. unlocked this conversation on Sep 26, 2024
  14. locked and limited conversation to collaborators on Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

db: postgres

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions