Opened 7 years ago

Closed 7 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 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 Jeff Cohen, 7 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 Leo, 7 years ago

Cc: Leo added

comment:3 by Mariusz Felisiak, 7 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 add DEFAULT clause when adding a new column to fill existing rows, but we also immediately drop it after that (see ​add_field()).

Note: See TracTickets for help on using tickets.
Back to Top