SelectAll Overwrites column with same field name in a join

Hello, very odd issue here. I have a table Course with field 'id' and User with field 'id'. When I join these two tables and SelectAll(['Course', 'User']), there's only a single id field, with the value from User. Is this a bug, or am I doing something wrong? (Detailed snippets below).
const result = await db
.selectFrom('Course')
.leftJoin('User', 'Course.instructorId', 'User.id')
.selectAll('Course')
.executeTakeFirstOrThrow();
const result = await db
.selectFrom('Course')
.leftJoin('User', 'Course.instructorId', 'User.id')
.selectAll('Course')
.executeTakeFirstOrThrow();
result: { id: 'a0no2ltsuo8chdfaws5l2arqyzhcjp', // <-- this is the actual course ID instructorId: 'dabkdqgh29j4cfkef3u1b8p63zcjhj', title: 'Rustic Wooden Ball' }
const result = await db
.selectFrom('Course')
.leftJoin('User', 'Course.instructorId', 'User.id')
.selectAll(['Course', 'User']) // <-- only change
.executeTakeFirstOrThrow();
const result = await db
.selectFrom('Course')
.leftJoin('User', 'Course.instructorId', 'User.id')
.selectAll(['Course', 'User']) // <-- only change
.executeTakeFirstOrThrow();
result: { id: 'dabkdqgh29j4cfkef3u1b8p63zcjhj', // <-- this is now the user id instructorId: 'dabkdqgh29j4cfkef3u1b8p63zcjhj', title: 'Rustic Wooden Ball', name: 'Penny Stroman', }
Solution:
Kysely is not an ORM. It doesn't magically rename things. You need to provide non-colliding names.
Jump to solution
3 Replies
koskimas
koskimas6mo ago
That's just how SQL works Both returned columns have the same name id and the latter overwrites the former How could anything else possibly happen?
Solution
koskimas
koskimas6mo ago
Kysely is not an ORM. It doesn't magically rename things. You need to provide non-colliding names.
Shawn Long
Shawn Long6mo ago
@koskimas, gotcha. I was expecting it to auto-rename the field if there was a duplicate in the results, but I get why that would be out of scope for Kysely. Appreciate it.
Want results from more Discord servers?
Add your server