Skip to content

Commit 1938818

Browse files
committed
address bugbot concerns
1 parent 2818b74 commit 1938818

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

apps/sim/app/api/table/[tableId]/rows/route.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,27 @@ export async function PUT(request: NextRequest, { params }: TableRowsRouteParams
469469
}
470470
}
471471

472-
// Check unique constraints using optimized database query
473472
const uniqueColumns = getUniqueColumns(table.schema as TableSchema)
474473
if (uniqueColumns.length > 0) {
474+
// If updating multiple rows, check that updateData doesn't set any unique column
475+
// (would cause all rows to have the same value, violating uniqueness)
476+
if (matchingRows.length > 1) {
477+
const uniqueColumnsInUpdate = uniqueColumns.filter((col) => col.name in updateData)
478+
if (uniqueColumnsInUpdate.length > 0) {
479+
return NextResponse.json(
480+
{
481+
error: 'Cannot set unique column values when updating multiple rows',
482+
details: [
483+
`Columns with unique constraint: ${uniqueColumnsInUpdate.map((c) => c.name).join(', ')}. ` +
484+
`Updating ${matchingRows.length} rows with the same value would violate uniqueness.`,
485+
],
486+
},
487+
{ status: 400 }
488+
)
489+
}
490+
}
491+
492+
// Check unique constraints against database for each row
475493
for (const row of matchingRows) {
476494
const existingData = row.data as RowData
477495
const mergedData = { ...existingData, ...updateData }

apps/sim/app/api/table/[tableId]/rows/upsert/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { db } from '@sim/db'
22
import { userTableRows } from '@sim/db/schema'
33
import { createLogger } from '@sim/logger'
4-
import { and, eq, sql } from 'drizzle-orm'
4+
import { and, eq, or, sql } from 'drizzle-orm'
55
import { type NextRequest, NextResponse } from 'next/server'
66
import { z } from 'zod'
77
import { checkHybridAuth } from '@/lib/auth/hybrid'
@@ -97,7 +97,7 @@ export async function POST(request: NextRequest, { params }: UpsertRouteParams)
9797
and(
9898
eq(userTableRows.tableId, tableId),
9999
eq(userTableRows.workspaceId, validated.workspaceId),
100-
...validUniqueFilters
100+
or(...validUniqueFilters)
101101
)
102102
)
103103
.limit(1)

0 commit comments

Comments
 (0)