joins overwrite select?

Am I crazy or this is not supposed to happen? My schema is:
export const pets = sqliteTable('pets', {
id: text({ length:12 }).primaryKey().$default(() => nanoid()),
owner_id: text().notNull().references(() => owners.id, { onDelete: 'cascade' }),
name: text().notNull(),
})

export const owners = sqliteTable('owners', {
id: text({ length:12 }).primaryKey().$default(() => nanoid()),
name: text().notNull(),
})
export const pets = sqliteTable('pets', {
id: text({ length:12 }).primaryKey().$default(() => nanoid()),
owner_id: text().notNull().references(() => owners.id, { onDelete: 'cascade' }),
name: text().notNull(),
})

export const owners = sqliteTable('owners', {
id: text({ length:12 }).primaryKey().$default(() => nanoid()),
name: text().notNull(),
})
When I do:
const pet_id = 122
const { ...pet_columns } = getTableColumns(pets)
const [ pet ] = await db
.select({ ...pet_columns, owner_name: owners.name })
.from(pets)
.innerJoin(owners, eq(pets.owner_id, owners.id))
.where( eq(pets.id, pet_id) )
.limit(1)
const pet_id = 122
const { ...pet_columns } = getTableColumns(pets)
const [ pet ] = await db
.select({ ...pet_columns, owner_name: owners.name })
.from(pets)
.innerJoin(owners, eq(pets.owner_id, owners.id))
.where( eq(pets.id, pet_id) )
.limit(1)
Data get's mixup, owner_id is assigned to pet id and also the names
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?