Skip to content

Commit a9c3629

Browse files
committed
fix: use ?columns= on upsert
1 parent 5ad7562 commit a9c3629

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/PostgrestQueryBuilder.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ export default class PostgrestQueryBuilder<
194194
* numbers.
195195
*
196196
* @param options.defaultToNull - Make missing fields default to `null`.
197-
* Otherwise, use the default value for the column.
197+
* Otherwise, use the default value for the column. This only applies when
198+
* inserting new rows, not when merging with existing rows under
199+
* `ignoreDuplicates: false`.
198200
*/
199201
upsert<Row extends Relation extends { Insert: unknown } ? Relation['Insert'] : never>(
200202
values: Row | Row[],
@@ -226,6 +228,14 @@ export default class PostgrestQueryBuilder<
226228
}
227229
this.headers['Prefer'] = prefersHeaders.join(',')
228230

231+
if (Array.isArray(values)) {
232+
const columns = values.reduce((acc, x) => acc.concat(Object.keys(x)), [] as string[])
233+
if (columns.length > 0) {
234+
const uniqueColumns = [...new Set(columns)].map((column) => `"${column}"`)
235+
this.url.searchParams.set('columns', uniqueColumns.join(','))
236+
}
237+
}
238+
229239
return new PostgrestFilterBuilder({
230240
method,
231241
url: this.url,

0 commit comments

Comments
 (0)