Opened 5 years ago
Closed 5 years ago
#30642 closed Cleanup/optimization (invalid)
Unable to include default value in column_sql when creating new model.
Reported by: | Jeff Cohen | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Leo Shklovskii | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The column_sql accepts an include_default argument, which is defaulted to False if it is not passed.
https://github.com/django/django/blob/master/django/db/backends/base/schema.py#L202
The code that calls it in table_sql, however, doesn't pass the argument so always defaults to False.
https://github.com/django/django/blob/master/django/db/backends/base/schema.py#L155
As a result, it does not appear possible to turn on that flag for migrations.
I have got this functionality working by subclassing the DatabaseSchemaEditor and overriding the column_sql
function to default include_default=True
.
Change History (3)
comment:1 by , 5 years ago
Summary: | Unable to include default value in column_sql → Unable to include default value in column_sql when creating new model |
---|
comment:2 by , 5 years ago
Cc: | added |
---|
comment:3 by , 5 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Summary: | Unable to include default value in column_sql when creating new model → Unable to include default value in column_sql when creating new model. |
Type: | Bug → Cleanup/optimization |
Version: | 2.2 → master |
Thanks for this report, however it is not a bug, it's rather a design decision.
Django (for now) doesn't support default values on database level (see discussion). That's why we don't add
DEFAULT
clause on database creation because no rows would be affected. We addDEFAULT
clause when adding a new column to fill existing rows, but we also immediately drop it after that (see add_field()).