© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
3 replies
kinsyu

onConflictDoUpdate excluded with a dynamic column

Hey, I'm trying to perform an insert where it's possible that the record already exists, therefore I'm using
onConflictDoUpdate
onConflictDoUpdate
, the problem I'm running into is that I can't dynamically set the column to update, here's my current approach:
export async function editGroupSettings(
  db: Db,
  option: string,
  groupId: number,
  userId: string,
  value: number,
) {
  await db
    .insert(groupAlertsConfigTable)
    .values({
      groupId: groupId,
      userId: userId,
      [option]: value,
    })
    .onConflictDoUpdate({
      target: [groupAlertsConfigTable.mafiaId, groupAlertsConfigTable.userId],
      set: {
        [option]: sql`EXCLUDED."${option}"`,
      },
      where: and(
        eq(groupAlertsConfigTable.groupId, groupId),
        eq(groupAlertsConfigTable.userId, userId),
      ),
    });
}

editGroupSettings(db, "minMCap", 1, "ry73wkah5ojhl76k", 2);
export async function editGroupSettings(
  db: Db,
  option: string,
  groupId: number,
  userId: string,
  value: number,
) {
  await db
    .insert(groupAlertsConfigTable)
    .values({
      groupId: groupId,
      userId: userId,
      [option]: value,
    })
    .onConflictDoUpdate({
      target: [groupAlertsConfigTable.mafiaId, groupAlertsConfigTable.userId],
      set: {
        [option]: sql`EXCLUDED."${option}"`,
      },
      where: and(
        eq(groupAlertsConfigTable.groupId, groupId),
        eq(groupAlertsConfigTable.userId, userId),
      ),
    });
}

editGroupSettings(db, "minMCap", 1, "ry73wkah5ojhl76k", 2);

I'm getting the following error when executing this
PostgresError: column excluded.$4 does not exist
PostgresError: column excluded.$4 does not exist
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

upsert with excluded using column name
Drizzle TeamDTDrizzle Team / help
2y ago
onConflictDoUpdate
Drizzle TeamDTDrizzle Team / help
5w ago
Insert Schemas with onConflictDoUpdate
Drizzle TeamDTDrizzle Team / help
3y ago
Can't upsert with onConflictDoUpdate & generatedAlwaysAsIdentity ID columns?
Drizzle TeamDTDrizzle Team / help
14mo ago