kal
kal
Explore posts from servers
DTDrizzle Team
Created by kal on 6/19/2024 in #help
How to properly handle/distinguish between errors
I'm using Drizzle ORM with a postgres. I'd like to distinguish between certain errors that occur when inserting / querying the database. In my current case, I would like to know specifically when my insertion fails as a result of a user not existing in the database. (See the comments in the catch block in the following example)
/**
*
* @param targetID the ID of the user to receive the friend request
*/
async sendUserFriendRequest(targetID: string): Promise<boolean> {
try {
await db
.insert(friends)
.values({
user1_ID: this.userID,
user2_ID: targetID,
status: "pending",
})
.onConflictDoNothing({ target: [friends.user1_ID, friends.user2_ID] });

log("social").debug(
`user ${this.userID} sent friend request to user ${targetID}`,
);
} catch (e) {
// if e is caused by targetID user not existing
// return false
// else
log("social").error(
`user '${this.userID}' failed to send friend request to user '${targetID}' %o`,
e,
);
return false;
}

return true;
}
/**
*
* @param targetID the ID of the user to receive the friend request
*/
async sendUserFriendRequest(targetID: string): Promise<boolean> {
try {
await db
.insert(friends)
.values({
user1_ID: this.userID,
user2_ID: targetID,
status: "pending",
})
.onConflictDoNothing({ target: [friends.user1_ID, friends.user2_ID] });

log("social").debug(
`user ${this.userID} sent friend request to user ${targetID}`,
);
} catch (e) {
// if e is caused by targetID user not existing
// return false
// else
log("social").error(
`user '${this.userID}' failed to send friend request to user '${targetID}' %o`,
e,
);
return false;
}

return true;
}
The error I get when a user does not exist is like so:
{
[message]: 'insert or update on table "friends" violates foreign key constraint "friends_user2_user_id_fk"',
length: 256,
name: 'error',
severity: 'ERROR',
code: '23503',
detail: 'Key (user2)=(hi) is not present in table "user".',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'friends',
column: undefined,
dataType: undefined,
constraint: 'friends_user2_user_id_fk',
file: 'ri_triggers.c',
line: '2619',
routine: 'ri_ReportViolation'
}
{
[message]: 'insert or update on table "friends" violates foreign key constraint "friends_user2_user_id_fk"',
length: 256,
name: 'error',
severity: 'ERROR',
code: '23503',
detail: 'Key (user2)=(hi) is not present in table "user".',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: 'public',
table: 'friends',
column: undefined,
dataType: undefined,
constraint: 'friends_user2_user_id_fk',
file: 'ri_triggers.c',
line: '2619',
routine: 'ri_ReportViolation'
}
What is the proper / best way to differentiate between causes of errors while using drizzle?
1 replies
DTDrizzle Team
Created by kal on 4/19/2024 in #help
Infer typescript type from table with relations
No description
4 replies
DTDrizzle Team
Created by kal on 4/11/2024 in #help
How to reference composite key in foreign key
I'm trying to create a foreign key which points to another tables composite primary key. I cannot seem to find anywhere how this is meant to be achieved. I have attached a simplified version of the code I am trying to get to work. As you can see in the moves table I am creating a composite primary key. How do I then reference that in the moveTimestamps table. Also, what should I put in place of varchar in the moveTimestamps table move column, typically I would just match the data type as what it is referencing, but in thise case it should reference both an interger and a varchar.
const moves = pgTable(
"moves",
{
gameID: varchar("game_id")
.notNull()
.references(() => games.id, { onDelete: "cascade" }),
turn: integer("turn").notNull().unique(), },
(table) => {
return {
pk: primaryKey({ name: "id", columns: [table.gameID, table.turn] }),
};
},
);
const moves = pgTable(
"moves",
{
gameID: varchar("game_id")
.notNull()
.references(() => games.id, { onDelete: "cascade" }),
turn: integer("turn").notNull().unique(), },
(table) => {
return {
pk: primaryKey({ name: "id", columns: [table.gameID, table.turn] }),
};
},
);
export const moveTimestamps = pgTable("moveTimestamps", {
move: varchar("move")
.notNull()
.references(() => //!What do i put here!)
.primaryKey(),
});
export const moveTimestamps = pgTable("moveTimestamps", {
move: varchar("move")
.notNull()
.references(() => //!What do i put here!)
.primaryKey(),
});
6 replies