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

select statement that references associations only is broken #968

Open
dimrat opened this issue Jan 7, 2025 · 1 comment
Open

select statement that references associations only is broken #968

dimrat opened this issue Jan 7, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@dimrat
Copy link

dimrat commented Jan 7, 2025

Description of erroneous behaviour

Select statements that reference associations only seem to be broken. Below SELECT for the standard cap bookshop sample (cds init <my-project> --add data,sample) fails

> await SELECT`books{ID, descr}`.from(Authors).where({ID:101})
Uncaught Error: "descr" not found in the elements of "sap.capire.bookshop.Authors"

If i add a field from the Authors entity, the SELECT works.

> await SELECT`ID,books{ID, descr}`.from(Authors).where({ID:101})
[
  {
    ID: 101,
    books: [
      {
        ID: 201,
        descr: `Wuthering Heights, Emily Brontë's only novel, was published in 1847 under the pseudonym "Ellis Bell". It was written between October 1845 and June 1846. Wuthering Heights and Anne Brontë's Agnes Grey were accepted by publisher Thomas Newby before the success of their sister Charlotte's novel Jane Eyre. After Emily's death, Charlotte edited the manuscript of Wuthering Heights and arranged for the edited version to be published as a posthumous second edition in 1850.`
      }
    ]
  }
]

Detailed steps to reproduce

  1. cds init test --add sample,data && cd test
  2. npm install
  3. cds repl --run .
    4 await SELECTbooks{ID, descr}.from(Authors).where({ID:101})

Details about your project

@cap-js/asyncapi 1.0.2
@cap-js/cds-types 0.6.5
@cap-js/db-service 1.16.2
@cap-js/openapi 1.1.1
@cap-js/sqlite 1.7.8
@sap/cds 8.6.0
@sap/cds-compiler 5.6.0
@sap/cds-dk (global) 8.6.1
@sap/cds-fiori 1.2.8
@sap/cds-foss 5.0.1
@sap/cds-mtxs 2.4.2
@sap/eslint-plugin-cds 3.1.2
Node.js v20.17.0
@dimrat dimrat added the bug Something isn't working label Jan 7, 2025
@patricebender
Copy link
Member

patricebender commented Jan 7, 2025

Hi,

indeed, the query is not properly parsed:

> q = SELECT`books{ID, descr}`.from(Authors).where({ID:101})
cds.ql {
  SELECT: {
    from: { ref: [ 'sap.capire.bookshop.Authors' ] },
    columns: [ { ref: [ 'ID' ] }, { ref: [ 'descr' ] } ],
    where: [ { ref: [ 'ID' ] }, '=', { val: 101 } ]
  }
}

you can workaround this with a slightly different cds.ql syntax:

> q = SELECT`from ${Authors} { books{ID, descr} }`.where({ID:101})
cds.ql {
  SELECT: {
    from: { ref: [ 'sap.capire.bookshop.Authors' ] },
    columns: [
      {
        ref: [ 'books' ],
        expand: [ { ref: [ 'ID' ] }, { ref: [ 'descr' ] } ]
      }
    ],
    where: [ { ref: [ 'ID' ] }, '=', { val: 101 } ]
  }
}

This looks like a regression in @sap/[email protected], it works with the following versions:

❯ cds --version

…
@sap/cds: 8.5.1
…
> q = SELECT`books{ID, descr}`.from(Authors).where({ID:101})
Query {
  SELECT: {
    from: { ref: [ 'sap.capire.bookshop.Authors' ] },
    columns: [
      {
        ref: [ 'books' ],
        expand: [ { ref: [ 'ID' ] }, { ref: [ 'descr' ] } ]
      }
    ],
    where: [ { ref: [ 'ID' ] }, '=', { val: 101 } ]
  }
}
> await q
[
  {
    books: [
      {
        ID: 201,
        descr: `Wuthering Heights, Emily Brontë's only novel, was published in 1847 under the pseudonym "Ellis Bell". It was written between October 1845 and June 1846. Wuthering Heights and Anne Brontë's Agnes Grey were accepted by publisher Thomas Newby before the success of their sister Charlotte's novel Jane Eyre. After Emily's death, Charlotte edited the manuscript of Wuthering Heights and arranged for the edited version to be published as a posthumous second edition in 1850.`
      }
    ]
  }
]

@danjoa this could be related to your recent efforts on cds.ql.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants