A query builder for PostgreSQL, MySQL and SQLite3, designed to be flexible, portable, and fun to use.
createTable
works, but not hasTable
createTableIfNotExists
is fine though
await knex.raw(...)
or knex.raw(...).then()
should
.then()
was mandatory. but that is not the case right? await should work as well? even the native await?
knex.raw
I keep getting a TypeError: db_1.knex.raw is not a function
error, any ideas how I can debug that? i'm on knex@0.21.18 and node 16.10.0. thanks
error error: select distinct "strapi_permission".* from "strapi_permission" left join "pgbouncer_dev-strapi"."public"."strapi_role" as "strapi_role_1" on "strapi_role_1"."id" = "strapi_permission"."role" where "strapi_role_1"."id" = $1 - cross-database references are not implemented: "pgbouncer_dev-strapi.public.strapi_role"
at Parser.parseErrorMessage (/home/unomi/coder/projects/flexstay/github/ps-cms/node_modules/pg-protocol/dist/parser.js:287:98)
From looking at previous issues and StackOverflow questions, it seems like the following should work:
const query = /* build a select query as a knex QueryBuilder*/
await connection('table').insert(query);
When I do this in a TypeScript environment, I get the following:
Argument of type 'QueryBuilder<any, any>' is not assignable to parameter of type 'DbRecordArr<TRecord> | readonly DbRecordArr<TRecord>[]'.
Type 'QueryBuilder<any, any>' is not assignable to type 'Readonly<Readonly<Partial<MaybeRawRecord<TRecord>>>>'.
Index signature for type 'string' is missing in type 'QueryBuilder<any, any>'.ts(2345)
The code also appears to execute succesfully -- am I doing something wrong, or is there maybe a type definition issue in the knex package?
I'm having some trouble with typescript typing on my query. I have an interface interface User { id: number; }
and a query like the one from the documentation like const res = await knex.select(knex.ref("id").withSchema("users")).from<User>("users");
The documentation (http://knexjs.org/guide/query-builder.html#select) says that this should be inferred as Pick<User, "id">[]
but for me it's getting inferred as any[]
. Changing the query to const res = await knex<User>("users").select(knex.ref("id").withSchema("users"));
results in the type inference being const res: { id: number; }[]
which is somewhat better but still not Pick<...>
as expected.
The real problem comes when I try to do a join with this. In order to get the typescript correct it seems like I have to do something like .innerJoin<Pick<User, "id">>(...)
in order to get it to work right (and of course in reality I'm selecting a bunch of fields so this quickly becomes very verbose, hard to read, and somewhat silly because it seems like the types aren't actually enforced if they aren't inferred). Am I doing something wrong or is inference just not able to cover complex queries?
Hi !
I'm trying to get the SQL code executed for a schema.alterTable with a .toString(), but when I add a table.renameColumn('name', 'pseudo') in the alter table, I can't get the complete SQL to rename the column, it only shows the first step of the rename :show full fields from `user` where field = 'name'
I know that the other step to rename the column requires the result of this query, and I would like to find a way to get the whole SQL after the execution of the alterTable
It's also not displayed with debug: true on the Knex instance
Thank you !