Shawn Long
Shawn Long
Explore posts from servers
KKysely
Created by Shawn Long on 5/7/2024 in #help
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', }
8 replies
KKysely
Created by Shawn Long on 3/31/2024 in #help
Separating results of join into objects of each type
Hey guys, I'm curious if anyone knows a way to unmerge the results of a join into objects of both types. For example, if you have a table User and table Bio, and query a single row which joins those two, the result is an a flat object with keys from both tables. What I'd like to do is separate the result into a User object and Bio object. Solutions that work but I don't love: 1. manually extracting values from the object based on the current schema. (future changes to schema will break this, so I'd like to avoid that) 2. Multiple database queries for each object (this lengthens load times, which isn't a great solution for me either). Let me know what you guys think and if you were able to find a good solution for this, or if I should just bite the bullet and go with solution 1.
5 replies