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

Parameter type inference not working for VALUES tables? #842

Closed
darkgnotic opened this issue Apr 6, 2024 · 2 comments
Closed

Parameter type inference not working for VALUES tables? #842

darkgnotic opened this issue Apr 6, 2024 · 2 comments

Comments

@darkgnotic
Copy link

darkgnotic commented Apr 6, 2024

For a select query with a parameter, an integer value is correctly identified as an INT4 (type = 23):

await sql`CREATE TABLE foo (id int)`;

console.log(
  await sql`SELECT * from foo WHERE id = ${1}`.describe(),
);

//      {
//        string: 'SELECT * from foo WHERE id = $1',
//        types: [ 23 ],
//        name: '9njwfoixz23',
//        columns: [
//          {
//            name: 'id',
//            parser: [Function: parse],
//            table: 462973,
//            number: 1,
//            type: 23
//         }
//        ]
//      }

However, for a query over a VALUES table, the same parameterized value is identified as TEXT (type = 25):

console.log(
  await sql`WITH bar (id) AS (VALUES (${1})) SELECT * FROM bar`.describe(),
);

//      {
//        string: 'WITH bar (id) AS (VALUES ($1)) SELECT * FROM bar',
//        types: [ 25 ],
//        name: 'owdbjhlx58l2',
//        columns: [ { name: 'id', parser: undefined, table: 0, number: 0, type: 25 } ]
//      }

And the result is similarly incorrect ({ id: '1' }).

Is this expected, and if so, is there a way to infer the value type from the JavaScript type?

@darkgnotic darkgnotic changed the title Parameter type inference not working for temporary tables? Parameter type inference not working for VALUES tables? Apr 6, 2024
@porsager
Copy link
Owner

porsager commented Apr 6, 2024

Yes, that's expected, and v4 will most likely drop any kind of lazy js inference completely as it is was a bad idea in the first place.

Check out
#392

For your example you'd solve it like this:


WITH bar (id) AS (VALUES (${1}::int)) SELECT * FROM bar

@darkgnotic
Copy link
Author

Thank you for the quick response and workaround! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants