Skip to content

Commit 25a9658

Browse files
committed
Make insert_and_get work for ConflictAction.NOTHING
1 parent 3f77fa6 commit 25a9658

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

psqlextra/manager.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,24 @@ def insert_and_get(self, **fields):
106106
"""
107107

108108
if not self.conflict_target and not self.conflict_action:
109+
print('oh fuck');
109110
# no special action required, use the standard Django create(..)
110111
return super().create(**fields)
111112

112113
compiler = self._build_insert_compiler(**fields)
113114
rows = compiler.execute_sql(return_id=False)
114-
column_data = rows[0]
115+
116+
columns = rows[0]
117+
118+
# it could happen that in the case of ConflictAction.NOTHING, the
119+
# row already existed, and in that case, we have to do an extra SELECT
120+
if not columns:
121+
select_fields = {
122+
field: fields[field]
123+
for field in self.conflict_target
124+
}
125+
126+
return self.get(**select_fields)
115127

116128
# get a list of columns that are officially part of the model
117129
model_columns = [
@@ -122,7 +134,7 @@ def insert_and_get(self, **fields):
122134
# strip out any columns/fields returned by the db that
123135
# are not present in the model
124136
model_init_fields = {}
125-
for column_name, column_value in column_data.items():
137+
for column_name, column_value in columns.items():
126138
if column_name not in model_columns:
127139
continue
128140

0 commit comments

Comments
 (0)