diff -r 3758c6563608 django/db/backends/__init__.py
a
|
b
|
|
52 | 52 | uses_custom_query_class = False |
53 | 53 | empty_fetchmany_value = [] |
54 | 54 | update_can_self_select = True |
| 55 | supports_return_id_from_insert = False |
55 | 56 | |
56 | 57 | class BaseDatabaseOperations(object): |
57 | 58 | """ |
diff -r 3758c6563608 django/db/backends/postgresql_psycopg2/base.py
a
|
b
|
|
23 | 23 | class DatabaseFeatures(BaseDatabaseFeatures): |
24 | 24 | needs_datetime_string_cast = False |
25 | 25 | needs_upper_for_iops = True |
| 26 | supports_return_id_from_insert = True |
26 | 27 | |
27 | 28 | class DatabaseOperations(PostgresqlDatabaseOperations): |
28 | 29 | def last_executed_query(self, cursor, sql, params): |
diff -r 3758c6563608 django/db/models/sql/subqueries.py
a
|
b
|
|
294 | 294 | result = ['INSERT INTO %s' % qn(self.model._meta.db_table)] |
295 | 295 | result.append('(%s)' % ', '.join([qn(c) for c in self.columns])) |
296 | 296 | result.append('VALUES (%s)' % ', '.join(self.values)) |
| 297 | if self.connection.features.supports_return_id_from_insert: |
| 298 | result.append('RETURNING %s.%s' % (qn(self.model._meta.db_table), |
| 299 | qn(self.model._meta.pk.column))) |
| 300 | |
297 | 301 | return ' '.join(result), self.params |
298 | 302 | |
299 | 303 | def execute_sql(self, return_id=False): |
300 | 304 | cursor = super(InsertQuery, self).execute_sql(None) |
301 | | if return_id: |
| 305 | if return_id and self.connection.features.supports_return_id_from_insert: |
| 306 | row_id = cursor.fetchone()[0] |
| 307 | return row_id |
| 308 | elif return_id: |
302 | 309 | return self.connection.ops.last_insert_id(cursor, |
303 | 310 | self.model._meta.db_table, self.model._meta.pk.column) |