knex icon indicating copy to clipboard operation
knex copied to clipboard

[BUG] Cross Join Incorrect Parameter Type

Open MFinn87 opened this issue 2 years ago • 1 comments

Environment

Knex version: 2.5.1 or 2.4.2 Database + version: Postgres 14 OS: All

@lorefnon

Bug

  1. Explain what kind of behaviour you are getting and how you think it should do

Expected Functionality: Per documentation, the .crossJoin function should allow a developer to specify a string when only 1 argument is supplied. 38881D05-03F1-4A29-8A8B-965D2419D736

Actual Functionality: When the 1st and only argument of the .crossJoin function is a string, then TypeScript throws an error, because the function parameter is mis-typed. The type definition defines this function as only accepting a type returned by knex.raw. I'm not sure if that type is correct or needs to stay, but it should at least also accept a string option.

  await knex
  .select('*')
  .from('team')
  .crossJoin('setting') // Knex type bug

7C4A3D9A-8385-4470-A8DE-060FEA9332D3

Here's a workaround:

  await knex
  .select('*')
  .from('team')
  .crossJoin('setting' as unknown as Knex.Raw) // Workaround

This workaround executes perfectly fine and the query runs with the cross join correctly, it just fools TypeScript into thinking there's no problem with hacky re-casting.

  1. Error message
src/database/seeds/index.ts:198:14 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'Raw<any>'.

198   .crossJoin('setting') // Knex type bug
                 ~~~~~~~~~

Found 1 error in src/database/seeds/index.ts:198

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
[nodemon] app crashed - waiting for file changes before starting...
  1. Reduced test code, for example in https://npm.runkit.com/knex or if it needs real database connection to MySQL or PostgreSQL, then single file example which initializes needed data and demonstrates the problem.

https://replit.com/@devmikef/Knex-Cross-Join-Type-Bug#index.ts Click the green Run button

Lastly, I just want to give a huge thanks for this amazing project. I've seen Knex make many teams more productive and help bring products to fruition that otherwise would have been much more difficult.

MFinn87 avatar Jul 26 '23 06:07 MFinn87

I'm also facing the same problem. Do you have any plans to fix this any time soon?

muhammetozendev avatar Apr 18 '25 19:04 muhammetozendev

Closing this in favour of #6248, which lists this issue (so the discussion isn't lost)

mercmobily avatar Aug 21 '25 03:08 mercmobily