#470: Add Field.db_default for defining database defaults
-------------------------------------+-------------------------------------
     Reporter:  jws                  |                    Owner:  Lily
                                     |  Foote
         Type:  New feature          |                   Status:  assigned
    Component:  Database layer       |                  Version:
  (models, ORM)                      |
     Severity:  normal               |               Resolution:
     Keywords:  sql schema           |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Adrian Torres):

 I would argue that there is no legitimate reason to want to set e.g. a
 literal value as default through python instead of through SQL.

 Query expressions seem to be supported by `Fields.default`, I haven't seen
 the actual implementation but I imagine it generates SQL that calculates a
 value and is set for every row in python, so it would make sense that
 these are always treated as a database default as well.

 Callables are not supported in the db_default implementation since it only
 makes sense to process them in python, calculating and setting the value
 for every row.

 The only case I can think of where having both `default` and `db_default`
 in a single field definition might be useful is having a default based on
 a python callable and having the db_default as a fallback in case the
 callable returned `None` or some "undesirable" value, but that seems
 complicated and far from the common case, IMO.

 For me having both `default` and `db_default` seems like an easy trap to
 fall into for beginners unaware of Django's implementation details, and
 for more seasoned developers a hassle and an easy mistake to make that can
 be costly.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/470#comment:60>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070184eda1350a-40e5bf4e-c2c2-45e2-b4f9-540d8012e314-000000%40eu-central-1.amazonses.com.

Reply via email to