Skip to content

Commit 0814153

Browse files
committed
Don't allow upsert on models without unique constraints
1 parent 0cdf141 commit 0814153

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

psqlextra/compiler.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.db.models.sql.compiler import SQLInsertCompiler
22

33
from .fields import HStoreField
4+
from django.core.exceptions import SuspiciousOperation
45

56

67
class PostgresSQLUpsertCompiler(SQLInsertCompiler):
@@ -57,7 +58,10 @@ def _rewrite_insert(self, sql, returning='id'):
5758
# a list of columns to pass in
5859
unique_columns = ', '.join(self._get_unique_columns())
5960
if len(unique_columns) == 0:
60-
return sql
61+
raise SuspiciousOperation((
62+
'You\'re trying to do a upsert on a table that '
63+
'doesn\'t have any unique columns.'
64+
))
6165

6266
# construct a list of columns to update when there's a conflict
6367
update_columns = ', '.join([

0 commit comments

Comments
 (0)