kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
9.81k stars 249 forks source link

Cant use raw table names with Insert/Update/Delete #1060

Closed zansonMH closed 1 week ago

zansonMH commented 1 week ago

I have an odd scenario with an older DB were slowly migrating to a new system. but in the meantime we need to support it with our new API. the problem is, this table name has a . in the name. so we need to wrap it in " in order to call it.

for examples, lets call this tables something_2.0

when selecting from this table, its a simple:

const table = sql<string>`"something_2.0"`;
Fastify.db.selectFrom(table).selectAll().where("id","=", id).execute()

this works great, but we can not do the same thing for inserts and updates, which are arguably the more important place to be using the builder.

this is the error using the same strategy with insertInto and updateTable, which makes sense because it only supports a raw string.

from.includes is not a function
    at parseTable (/home/zanson/projects/myh/fusion/ts-usurper-v5-edge-api/node_modules/kysely/dist/cjs/parser/table-parser.js:40:14)
    at Transaction.insertInto (/home/zanson/projects/myh/fusion/ts-usurper-v5-edge-api/node_modules/kysely/dist/cjs/query-creator.js:72:103)
    at DAOAgency.create (/home/zanson/projects/myh/fusion/ts-usurper-v5-edge-api/src/DAO/Pharaoh/List/DAOAgency.ts:81:25)
    at /home/zanson/projects/myh/fusion/ts-usurper-v5-edge-api/src/route/v5/auth/index.ts:186:34
    at /home/zanson/projects/myh/fusion/ts-usurper-v5-edge-api/node_modules/kysely/dist/cjs/kysely.js:376:38
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at DefaultConnectionProvider.provideConnection (/home/zanson/projects/myh/fusion/ts-usurper-v5-edge-api/node_modules/kysely/dist/cjs/driver/default-connection-provider.js:12:20)
    at Object.SimpleBenefitsHandler (/home/zanson/projects/myh/fusion/ts-usurper-v5-edge-api/src/route/v5/auth/index.ts:180:2)

if we use the table name straight up it creates a query like:

Fastify.db.inserInto("something_2.0").values({id:2, something:""}).execute()
insert into "something_2"."0" ("id", "something") values ($1, $2)
koskimas commented 1 week ago

The select query definitely doesn't work and shouldn't work https://kyse.link/XYN3x. Yes, you can compile it if you completely ignore type checks and the javascript works. But types definitely don't.

Neither should any other query. You haven't specified a table name at all and string is not a valid row type.