I'd be interested in seeing this. Generated columns are a useful SQL feature that are missing from Django.
Nice initial research on backend coverage - it looks like they're widely supported. Some ideas... Is it necessary to take a base field? Can we not determine the output field type for some kinds of expression? e.g. F("some_integer_field") + 1 can be assumed to have output field type IntegerField. Would it be worth blocking assignment to the field? Allowing "model.generated_field = ..." would at least be misleading. (This could be done with a special descriptor.) Good luck! On Wed, Apr 13, 2022 at 11:25 AM Paolo Melchiorre <pa...@melchiorre.org> wrote: > Hi all, > > I am at PyCon DE 2022 in Berlin with Markus and I shared with him this > idea that I have been thinking about for a few months, given his > interest I also share it with you. > > I figured we could add in Django a "GeneratedField" which accepts a > "base_field" attribute in a similar way to "ArrayField" and then an > expression to generate a value for the field. > > For example this model: > > class Album(models.Model): > ... > title = models.CharField(max_length=120) > search = GeneratedField( > SearchVectorField(), > F('title') > ) > > would generate an SQL code like this in PostgreSQL: > > CREATE TABLE album ( > ... > title char(120), > search tsvector GENERATED ALWAYS AS title STORED > ); > > I found some documentation for different database backend about > generated column (or similar names) > > # SQLite > https://www.sqlite.org/gencol.html > https://www.sqlite.org/releaselog/3_31_0.html > > # PostgreSQL > https://www.postgresql.org/docs/12/ddl-generated-columns.html > https://www.postgresql.org/docs/12/release-12.html > > # Oracle > https://oracle-base.com/articles/11g/virtual-columns-11gr1 > > # MySQL > https://dev.mysql.com/doc/refman/5.7/en/mysql-nutshell.html > https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html > > # MariaDB > https://mariadb.com/kb/en/changes-improvements-in-mariadb-102/ > https://mariadb.com/kb/en/generated-columns/ > > I'd love to hear your thoughts on this idea and if you think it might > be something worth continuing to investigate. > > Greetings from PyCon DE 2022 in Berlin, > Paolo > > -- > https://www.paulox.net > > -- > You received this message because you are subscribed to the Google Groups > "Django developers (Contributions to Django itself)" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-developers+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-developers/CAKFO%2Bx5GHUEVdzi2awYtH5C17tTPTPh%2ByoDP%3DKC18pF8%2Bi_7PA%40mail.gmail.com > . > -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAMyDDM2CWm0HOkLC%3DBjqQ92928L%3DD%3DXOZzUtA1cHdgc235oYRQ%40mail.gmail.com.