K
KyselydjMax

insert into with mix of static and table values

How might I execute an insert into that combines JS-side values with a select, like:
INSERT INTO some_table (a, b, c, d) SELECT o.a, o.b, 16, 'hello world' FROM other_table O WHERE somekey = 32;
INSERT INTO some_table (a, b, c, d) SELECT o.a, o.b, 16, 'hello world' FROM other_table O WHERE somekey = 32;
(Where, 16, 32 and 'hello world' are JS variables, not true literals)
I
Igal338d ago
Hey 👋 Something like this? https://kyse.link/?p=s&i=IEOBYyjLCvHjMa5JzUmN
import { sql } from "kysely"

const sixteen = 16
const thirthyTwo = 32
const helloWorld = "hello world"

const result = await kysely
.insertInto("some_table")
.columns(["a", "b", "c", "d"])
.expression(
kysely
.selectFrom("other_table as O")
.where("somekey", "=", thirthyTwo)
.select([
"O.a",
"O.b",
sql.val(sixteen).as("c"),
sql.val(helloWorld).as("d"),
]),
)
.execute()
import { sql } from "kysely"

const sixteen = 16
const thirthyTwo = 32
const helloWorld = "hello world"

const result = await kysely
.insertInto("some_table")
.columns(["a", "b", "c", "d"])
.expression(
kysely
.selectFrom("other_table as O")
.where("somekey", "=", thirthyTwo)
.select([
"O.a",
"O.b",
sql.val(sixteen).as("c"),
sql.val(helloWorld).as("d"),
]),
)
.execute()
D
djMax337d ago
Yes, like that! I thought it was kysely.val (as did GPT), but didn't see the sql one. Also weird that it needs the alias, but doable.
I
Igal337d ago
GPT doesn't know kysely. It always lies.
K
koskimas336d ago
Yep, Kysely didn't exist when the GPT training data was collected.
Want results from more Discord servers?
Add your server
More Posts
Migration error "TypeError: Cannot read properties of undefined (reading 'getExecutor')"I am trying to run a migration using Kysely, and its returning this error: ``` file:///Users/brunocrwhere clause with lengthHow do I have a where clause with a length, e.g. `select * from data where length(prodcode) = 3`show generated sqlhow can i see what the generated sql is for a kysely query? e.g. ``` result = await db.selectFrom("Property does not exist on typeHello. I'm trying to run the following query: ```javascript result = await db.selectFrom("data") Correct type definitions for function receiving builderI have something like this and I was wondering if it's the correct way to type my helper function. Transform Postgres array into JS arrayHey, I was wondering if there's a helper or something to transform Postgres arrays `{one, two}` intoNoob Question: SQL INSERT that combines static values with a SELECT statementHey Kysely community! I'm just getting into Kysely and I have a question about how I could run the fIs there a way to execute an ExpressionBuilder?Using the expression hasDogNamed example from the docs ``` const eb = expressionBuilder<DB, 'personArgument of type 'string' is not assignable to parameter of type 'DynamicReferenceBuilder<never>'.The following statement, is giving me the error on the title ```ts fn("round", [fn.min("ld.price_btcHow do Date columns works?I am learning Kysely from the website, at the getting started section I can see the following line: converting result.insertedId to numberwhen using INT as PK i can safe convert `result.insertedId` from `bigInt` to `Int` using `Number(resAdvice on building plugin for working with parametersI am currently working on a driver for YDB https://github.com/Gaspero/kysely-ydb YDB dialect expectMysql Query execution never endsHello, I have a question regarding my new integration of kysely in trpc. I wanted to port my prisma How to do join with raw sql snippets?How can I do this join with kysely? `left join videos on videos.id = CAST(highlights_content.item AS