can_introspect_default feature

161 views
Skip to first unread message

Maximiliano Robaina

unread,
Oct 18, 2016, 9:51:46 AM10/18/16
to Django developers (Contributions to Django itself)
Hi,

How supposed to can_introspect_default must work?

I'm working on django firebird backend and I'm trying to figured out how this feature works, but the schema tests fails. 
This default is at database level or django level?

If I define a field with a default value, and do a metadata introspection over this field to know if default value is defined, it always returns True.

I hope be clear (sorry for my bad english)

---
Maxi

Tim Graham

unread,
Oct 18, 2016, 11:07:34 AM10/18/16
to Django developers (Contributions to Django itself)
Hi Maxi,

Did you take a look at the relevant commit that introduced the flag?
https://github.com/django/django/commit/75303b01a9cc900eebf1f27ba0bc6508334242fc

Maximiliano Robaina

unread,
Oct 20, 2016, 8:29:29 AM10/20/16
to Django developers (Contributions to Django itself)
Hi Tim,

Thanks for response.


El martes, 18 de octubre de 2016, 12:07:34 (UTC-3), Tim Graham escribió:
Hi Maxi,

Did you take a look at the relevant commit that introduced the flag?
https://github.com/django/django/commit/75303b01a9cc900eebf1f27ba0bc6508334242fc

Yes, I was looking into this changeset, but I can't understand yet what "And that the default is no longer set in the database." means in test_add_field_default_dropped [1].
field.default contains the column default definition not the default value of the some table record, Is it that correct?, So, why this must be None?





Regards 

Tim Graham

unread,
Oct 20, 2016, 8:53:23 AM10/20/16
to Django developers (Contributions to Django itself)
The 'default' model field option is used in Python rather than in the database. SchemaEditor might set a column default to ease populating a value on existing rows but that default shouldn't be left in the column's definition. self.assertIsNone(field.default) means that the column doesn't have a default.

Chris Foresman

unread,
Oct 25, 2016, 3:39:54 PM10/25/16
to Django developers (Contributions to Django itself)
Out of curiosity, why isn't this added as part of the column definition? Isn't it better to enforce the default at the DB layer? Or is it to account for differences across database types? (This trips me up a lot because we have a lot of different apps/processes which have to touch the database and only the API server is written in Django.)

Tim Graham

unread,
Oct 25, 2016, 5:22:13 PM10/25/16
to Django developers (Contributions to Django itself)
It isn't always possible to transform arbitrary Python defaults (particularly callables) into SQL.

A search query would help you answer a question like this. I typed
"django database column defaults site:code.djangoproject.com" into Google and found
https://code.djangoproject.com/ticket/470 among other tickets.

...or searching the archives of this mailing list for "django database defaults" gives discussions such as:
https://groups.google.com/d/topic/django-developers/nrVzOnHBk64/discussion

Maximiliano Robaina

unread,
Apr 25, 2017, 1:21:47 PM4/25/17
to Django developers (Contributions to Django itself)
Hi,

Sorry but a need to return to this


El jueves, 20 de octubre de 2016, 9:53:23 (UTC-3), Tim Graham escribió:
The 'default' model field option is used in Python rather than in the database. SchemaEditor might set a column default to ease populating a value on existing rows but that default shouldn't be left in the column's definition. self.assertIsNone(field.default) means that the column doesn't have a default.


Ok, you say that SchemaEditor set a column default to ease populating a value on existing row. Which sql engine works in that way?
I was reading into postgresql docs [1] and it says that add a column default definition "Note that this doesn't affect any existing rows in the table, it just changes the default for future INSERT commands.". So, I don't undertend why add a database default definition is useful if later we need to remove it.

Shai Berger

unread,
Apr 25, 2017, 5:28:57 PM4/25/17
to django-d...@googlegroups.com
Hi Maxi,

On Tuesday 25 April 2017 20:21:47 Maximiliano Robaina wrote:
> Hi,
>
> Sorry but a need to return to this
>
> El jueves, 20 de octubre de 2016, 9:53:23 (UTC-3), Tim Graham escribió:
> > The 'default' model field option is used in Python rather than in the
> > database. SchemaEditor might set a column default to ease populating a
> > value on existing rows but that default shouldn't be left in the column's
> > definition. self.assertIsNone(field.default) means that the column
> > doesn't have a default.
>
> Ok, you say that SchemaEditor set a column default to ease populating a
> value on existing row. Which sql engine works in that way?

All of them, as far as I know.

> I was reading into postgresql docs [1] and it says that add a column
> default definition "Note that this doesn't affect any existing rows in the
> table, it just changes the default for future INSERT commands.". So, I
> don't undertend why add a database default definition is useful if later we
> need to remove it.
>

That statement is correct if it refers to adding a default definition to an
existing column, but SchemaEditor uses defaults only when it adds new columns.
if you add a new column to a table that already has rows, then the easiest way
to set a value for all existing rows is to define it as default. If the column
is not nullable, you actually cannot add it without a default.

And yet, some Python defaults cannot be represented in SQL (mostly functions),
and we'd much rather use Python defaults. Experience (in South) has shown that
defaults in the database, when used carelessly, cause problems, so we try to
keep them out.

HTH,
Shai.

Maximiliano Robaina

unread,
Apr 25, 2017, 7:47:41 PM4/25/17
to Django developers (Contributions to Django itself)

Hi Shai,

Yes, I understand that and I agree. I my case, Firebird has an strange behavior when you add a new not null field with a default  and then remove the default definition.
I must to implement other strategies.

Regards
Maxi


 

HTH,
        Shai.
Reply all
Reply to author
Forward
0 new messages