# GeneratedField with db\_persist=False creates a database column anyway

**URL:** <https://forum.djangoproject.com/t/generatedfield-with-db-persist-false-creates-a-database-column-anyway/32767>\
**Category:** Mystery Errors\
**Created:** [July 7, 2024, 7:23pm UTC](https://forum.djangoproject.com/t/generatedfield-with-db-persist-false-creates-a-database-column-anyway/32767 "2024-07-07T19:23:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![venteto](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/venteto/32/22412_2.png) [@venteto](https://forum.djangoproject.com/u/venteto)\
**Post date:** [July 7, 2024, 7:23pm UTC](https://forum.djangoproject.com/t/generatedfield-with-db-persist-false-creates-a-database-column-anyway/32767/1 "2024-07-07T19:23:01Z")

</div>

I have the following model:

```python
from django.db import models
from django.db.models import F, Func
from django.db.models.functions import Length
from django_reuse.abstract_models import Timestamped

class Painting(Timestamped):
    work_name = models.CharField(max_length=60)

    work_name_length = models.GeneratedField(
        # expression=Func(F('work_name'), function='Length'),
        expression=Length(F('work_name')),
        output_field=models.PositiveSmallIntegerField(),
        db_persist=False
    )

```

Everything seems to work okay, but when I open my Sqlite database, I see the generated field was persisted to the database anyway:

 ![Screenshot from 2024-07-07 15-19-43](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/8/4/8484060ea052af95972a4b6241bf177d33138264.png)

Not sure if I am doing something wrong, or if it is a bug, but if a column will be created in the database anyway, I would just use an IntegerField directly and do the calculation via overriding the save method.

I seem to get equivalent results, so is there any advantage to still using the newer field which seems to be broken, or perhaps just broken specfically with Sqlite?

```python
from django.db import models
from django.db.models import F # Func
from django.db.models.functions import Length
from django_reuse.abstract_models import Timestamped

class Painting(Timestamped):
    title = models.CharField(max_length=60)

    title_len = models.PositiveSmallIntegerField(blank=True)

    title_len_gen = models.GeneratedField(
        # expression=Func(F('title'), function='Length'),
        expression=Length(F('title')),
        output_field=models.PositiveSmallIntegerField(),
        db_persist=False
    )
    
    class Meta:
        ordering=['title']

    def __str__ (self):
        return self.title

    def save(self, *args, **kwargs):
        self.title_len = len(self.title)
        super(Painting, self).save(*args, **kwargs)

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 7, 2024, 10:41pm UTC](https://forum.djangoproject.com/t/generatedfield-with-db-persist-false-creates-a-database-column-anyway/32767/2 "2024-07-07T22:41:29Z")

</div>

How have you determined that a physical column has been created in the database? (Can you change that value and have it saved?)

Do you find this data in each row physically within that row?

You’re looking at the output of tool that shows all the columns in the table. From the perspective of the external API, that column _does_ exist. An SQL DML statement isn’t going to detect whether the column is real or virtual.

---

<div class="post-metadata">

**Author:** ![venteto](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/venteto/32/22412_2.png) [@venteto](https://forum.djangoproject.com/u/venteto)\
**Post date:** [July 7, 2024, 11:12pm UTC](https://forum.djangoproject.com/t/generatedfield-with-db-persist-false-creates-a-database-column-anyway/32767/3 "2024-07-07T23:12:54Z")

</div>

Okay, so from this screenshot I guess I can safely assume this “column” is in fact virtual rather than stored?

 ![Screenshot from 2024-07-07 19-11-53](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/4/a/4a09a76c8fe88421a6df63771765e416274d8e14.png)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 7, 2024, 11:27pm UTC](https://forum.djangoproject.com/t/generatedfield-with-db-persist-false-creates-a-database-column-anyway/32767/4 "2024-07-07T23:27:45Z")

</div>

I’m not sure whether that screenshot is definitive or not. That’s still working through the library’s API.

I think the only way to definitively answer your question would be to look at the structure of the db.sqlite3 file itself to see if the column exists physically in each row.

(Having said that, I’ve answered this question to my own satisfaction, but I don’t expect anyone else to accept my judgement on it.)

---

<div class="post-metadata">

**Author:** ![venteto](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/venteto/32/22412_2.png) [@venteto](https://forum.djangoproject.com/u/venteto)\
**Post date:** [July 8, 2024, 3:54pm UTC](https://forum.djangoproject.com/t/generatedfield-with-db-persist-false-creates-a-database-column-anyway/32767/5 "2024-07-08T15:54:49Z")

</div>

Well, opening the database file in Gedit, I see “Virtual” so I guess that’s enough for me, and based on docs elsewhere, it looks like Sqlite and MariaDB are the only options for me if I want a virtual column, I don’t see any plans from Postgres to support this in the near future.

 ![Screenshot from 2024-07-08 11-08-22](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/3/d/3dd1356888fb50254efe752cb646cf21579daa6a.png)
