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

Summary: Unable to include default value in column_sqlUnable to include default value in column_sql when creating new model

comment:2 by Leo Shklovskii, 5 years ago

Cc: Leo Shklovskii added

comment:3 by Mariusz Felisiak, 5 years ago

Resolution: invalid
Status: newclosed
Summary: Unable to include default value in column_sql when creating new modelUnable to include default value in column_sql when creating new model.
Type: BugCleanup/optimization
Version: 2.2master

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