Skip to content

Commit

Permalink
fix creation/deletion of unique_rowid() default when altering to/from…
Browse files Browse the repository at this point in the history
… AutoField

fixes #157
  • Loading branch information
timgraham committed Aug 4, 2020
1 parent dbf1eaf commit ce6abf7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,9 @@
- Enable `QuerySet.select_for_update()` and
`QuerySet.select_for_update(of=...)` in CockroachDB 20.1.

- Fix creation/deletion of `unique_rowid()` default when altering to/from
`AutoField`.

## 3.0.1 - 2020-05-20

- Add support for `QuerySet.explain()` options.
Expand Down
19 changes: 19 additions & 0 deletions django_cockroachdb/schema.py
Expand Up @@ -41,6 +41,25 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
self, model, old_field, new_field, old_type, new_type, old_db_params,
new_db_params, strict,
)
# Add or remove `DEFAULT unique_rowid()` for AutoField.
old_suffix = old_field.db_type_suffix(self.connection)
new_suffix = new_field.db_type_suffix(self.connection)
if old_suffix != new_suffix:
if new_suffix:
self.execute(self.sql_alter_column % {
'table': self.quote_name(model._meta.db_table),
'changes': 'ALTER COLUMN %(column)s SET %(expression)s' % {
'column': self.quote_name(new_field.column),
'expression': new_suffix,
}
})
else:
self.execute(self.sql_alter_column % {
'table': self.quote_name(model._meta.db_table),
'changes': 'ALTER COLUMN %(column)s DROP DEFAULT' % {
'column': self.quote_name(new_field.column),
}
})

def _alter_column_type_sql(self, model, old_field, new_field, new_type):
self.sql_alter_column_type = 'ALTER COLUMN %(column)s TYPE %(type)s'
Expand Down

0 comments on commit ce6abf7

Please sign in to comment.