iqrow
iqrow
DTDrizzle Team
Created by iqrow on 6/2/2023 in #help
Select One UX improvement
I have this
const arr = await db.select().from(worlds).where(eq(worlds.id, id)).limit(1)
const world = arr[0]
const arr = await db.select().from(worlds).where(eq(worlds.id, id)).limit(1)
const world = arr[0]
and that's fine and works, and great. but what I really want is to get rid of that second line. I don't want an array back if I know I'm only aiming to get a single record back. Is there a way to do this? Otherwise, let this be a little UX improvement suggestion and you can ignore it / adopt it as you see fit. Thanks!
8 replies
DTDrizzle Team
Created by iqrow on 5/24/2023 in #help
How to delete with cascade?
I'm using postgres with the following schema (reduced to the important parts):
export const worlds = pgTable('worlds', {
id: uuid('id').defaultRandom().primaryKey()
})
export const users_to_worlds = pgTable(
'users_to_worlds',
{
userId: varchar('user_id', { length: 32 })
.references(() => users.id)
.notNull(),
worldId: uuid('world_id')
.references(() => worlds.id)
.notNull(),
},
(table) => {
return {
pk: primaryKey(table.userId, table.worldId),
}
}
)
export const worlds = pgTable('worlds', {
id: uuid('id').defaultRandom().primaryKey()
})
export const users_to_worlds = pgTable(
'users_to_worlds',
{
userId: varchar('user_id', { length: 32 })
.references(() => users.id)
.notNull(),
worldId: uuid('world_id')
.references(() => worlds.id)
.notNull(),
},
(table) => {
return {
pk: primaryKey(table.userId, table.worldId),
}
}
)
And I'm trying to implement an api call to delete a world. Due to the reference to the world in the users_to_worlds I get the error: Error: update or delete on table "worlds" violates foreign key constraint "users_to_worlds_world_id_worlds_id_fk" on table "users_to_worlds" I believe what I want to use is a CASCADE delete where everything referencing a world is deleted when I delete a world. Is this possible through Drizzle? I can't seem to find anything on this.
7 replies