K
Kysely4mo ago
OverHash

bulk-insert with constant variable as one column

Hey, I'm basically trying to mirror https://stackoverflow.com/questions/6937442/in-postgres-is-it-possible-to-insert-a-constant-combined-with-the-result-of-sel. I can't figure out how I'd achieve this with Kysely. I'm currently here:
await trx
.insertInto("mapped_groups")
.columns(["group_id_fk", "mapped_id_fk"])
.expression((eb) =>
eb
.selectFrom("groups")
.select(["group_id_fk", sql`${insertedId}`])
.where("group_name", "in", groups),
);
await trx
.insertInto("mapped_groups")
.columns(["group_id_fk", "mapped_id_fk"])
.expression((eb) =>
eb
.selectFrom("groups")
.select(["group_id_fk", sql`${insertedId}`])
.where("group_name", "in", groups),
);
but this doesn't quite work on the select statement, since Kysely/TS doesn't like the second argument. Does anyone know how I could do a INSERT INTO ... SELECT ... query, where one of the values I am SELECTing is a constant (in my case, insertedId)?
Solution:
<#1239624163682029690> Had me covered! ```ts await trx .insertInto("mapped_groups") .columns(["group_id_fk", "mapped_id_fk"])...
Jump to solution
1 Reply
Solution
OverHash
OverHash4mo ago
<#1239624163682029690> Had me covered!
await trx
.insertInto("mapped_groups")
.columns(["group_id_fk", "mapped_id_fk"])
.expression((eb) =>
eb
.selectFrom("groups")
.select(["group_id_fk", eb.val(insertedId).as("mapped_id_fk")])
.where("group_name", "in", groups),
)
.execute();
await trx
.insertInto("mapped_groups")
.columns(["group_id_fk", "mapped_id_fk"])
.expression((eb) =>
eb
.selectFrom("groups")
.select(["group_id_fk", eb.val(insertedId).as("mapped_id_fk")])
.where("group_name", "in", groups),
)
.execute();
Want results from more Discord servers?
Add your server