# Creating a model with "GeneratedField" results in a DoesNotExist error.

**URL:** <https://forum.djangoproject.com/t/creating-a-model-with-generatedfield-results-in-a-doesnotexist-error/28249>\
**Category:** Mystery Errors\
**Created:** [February 20, 2024, 7:40am UTC](https://forum.djangoproject.com/t/creating-a-model-with-generatedfield-results-in-a-doesnotexist-error/28249 "2024-02-20T07:40:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![plusblanc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/plusblanc/32/19315_2.png) [@plusblanc](https://forum.djangoproject.com/u/plusblanc)\
**Post date:** [February 20, 2024, 7:40am UTC](https://forum.djangoproject.com/t/creating-a-model-with-generatedfield-results-in-a-doesnotexist-error/28249/1 "2024-02-20T07:40:34Z")

</div>

> class BaseModel(models.Model):
> 
> ```
> created_at = models.DateTimeField(_("created at"), auto_now_add=True, editable=False)
> updated_at = models.DateTimeField(_("updated at"), auto_now=True, editable=False)
> created_by = models.ForeignKey(
> settings.AUTH_USER_MODEL,
> verbose_name=_("created by"),
> on_delete=models.PROTECT,
> blank=True,
> null=True,
> editable=False,
> related_name="created_%(class)s",
> db_column="created_by",
> )
> updated_by = models.ForeignKey(
> settings.AUTH_USER_MODEL,
> verbose_name=_("updated by"),
> on_delete=models.PROTECT,
> blank=True,
> null=True,
> editable=False,
> related_name="updated_%(class)s",
> db_column="updated_by",
> )
> 
> class Meta:
> abstract = True
> 
> ```
> 
> > 
> 
> class LeadingZeroIDModel(models.Model):
> 
> ```
> class Meta:
> abstract = True
> 
> _id_length = 5 
> 
> def save(self, *args, **kwargs):
> with transaction.atomic():
> if not self.id and self._state.adding:
> id_length = kwargs.pop("id_length", self._id_length)
> self.id = self.generate_new_id(id_length)
> super().save(*args, **kwargs)
> 
> def generate_new_id(self, id_length):
> last_id = self. __class__.objects.all().values_list("id", flat=True).order_by("-id").first() or "0"
> new_id = str(int(last_id) + 1).zfill(id_length)
> return new_id
> 
> ```
> 
> class Company(BaseModel, LeadingZeroIDModel):
> 
> ```
> id = models.CharField(_("id"), primary_key=True, max_length=5, unique=True, editable=False)
> name = models.CharField(_("name"), max_length=20, unique=True)
> COUNTRY_CHOICES = {
> "JP": _("Japan"),
> "US": _("USA"),
> }
> country = models.CharField(_("country"), max_length=2, choices=COUNTRY_CHOICES, default="JP")
> staff_first_name = models.CharField(_("staff first name"), max_length=35)
> staff_last_name = models.CharField(_("staff last name"), max_length=35)
> 
> staff_full_name = models.GeneratedField(
> expression=Case(
> When(country="JP", then=Concat(F("staff_last_name"), F("staff_first_name"))),
> When(country="US", then=Concat(F("staff_first_name"), Value(" "), F("staff_last_name"))),
> ),
> output_field=CharField(max_length=70),
> db_persist=True,
> )
> 
> _id_length = 5
> 
> def __str__ (self):
> return self.name
> 
> class Meta:
> db_table = "company"
> verbose_name = _("company")
> 
> ```

When I try to create an object of this model, I get an error  
DoesNotExist at company/new/  
Company matching query does not exist.

CompanyCreateView is

> class CompanyCreateView(CreateView):  
> model = Company  
> template\_name = “company/company\_create.html”  
> form\_class = CompanyCreateForm
> 
> ```
> def get_success_url(self):
> return reverse_lazy("company-detail", kwargs={"pk": self.object.pk})
> 
> def form_valid(self, form):
> form.instance.created_by = form.instance.updated_by = self.request.user
> return super().form_valid(form)
> 
> ```

and CompanyCreateForm is

> class CompanyCreateForm(forms.ModelForm):
> 
> ```
> class Meta:
> model = Company
> fields = ["name", "staff_first_name", "staff_last_name"]
> 
> ```

If I remove “staff\_full\_name”, which is a “GeneratedField” from the model, and then migrate it, everything works fine. Why am I getting an error? I am using SQLite in the development phase.

---

<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:** [February 20, 2024, 1:08pm UTC](https://forum.djangoproject.com/t/creating-a-model-with-generatedfield-results-in-a-doesnotexist-error/28249/2 "2024-02-20T13:08:39Z")

</div>

You may be hitting one of the [SQLite restrictions on generated columns](https://www.sqlite.org/gencol.html#limitations).

I’m thinking #7 is a possibility, but you’d have to check your migrations to see if that’s the issue.

Side note: As a general rule, you typically want to specify mixins _before_ the base classes. In most cases, the intent of a mixin is to supersede the base class. (I’m not certain this is an issue _here_, but given that you’re overriding the model’s save method in `LeadingZeroDModel`, I would think that you do want it first in the `Company` class definition.)

---

<div class="post-metadata">

**Author:** ![plusblanc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/plusblanc/32/19315_2.png) [@plusblanc](https://forum.djangoproject.com/u/plusblanc)\
**Post date:** [February 21, 2024, 5:24am UTC](https://forum.djangoproject.com/t/creating-a-model-with-generatedfield-results-in-a-doesnotexist-error/28249/3 "2024-02-21T05:24:05Z")

</div>

Thanks for the comment. KenWhitesell.  
Looking at the part

> It is not possible to ALTER TABLE ADD COLUMN a STORED column. One can add a VIRTUAL column, however.

I tried changing “db\_persist” in GeneratedField to False, but I still get the same error. OK. I guess this doesn’t work with SQLite.

But for something like filtering objects by “staff\_full\_name”, that field is absolutely necessary (There may be other ways, but none come to mind, at least not for me.).

If I change the database to PostgreSQL or MySQL, will what I want work? Or will I have to try it out to find out?

Addendum: I changed the Company class to class Company(LeadingZeroIDModel, BaseModel) as per your advice. Thanks for your kind reply!

---

<div class="post-metadata">

**Author:** ![sarahboyce](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sarahboyce/32/12734_2.png) [@sarahboyce](https://forum.djangoproject.com/u/sarahboyce)\
**Post date:** [February 21, 2024, 9:58am UTC](https://forum.djangoproject.com/t/creating-a-model-with-generatedfield-results-in-a-doesnotexist-error/28249/4 "2024-02-21T09:58:56Z")

</div>

What version of Django do you have?  
There have been some patch releases for 5.0 around `GeneratedField`s, just want to check you have the latest patch.

Also minor thing, max\_length should be 71 if you do first and last name with a space 👍

---

<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:** [February 21, 2024, 11:09am UTC](https://forum.djangoproject.com/t/creating-a-model-with-generatedfield-results-in-a-doesnotexist-error/28249/5 "2024-02-21T11:09:55Z")

</div>

> [@plusblanc](#):
>
> I changed the Company class to class Company(LeadingZeroIDModel, BaseModel) as per your advice.

Actually, I made a mistake. I missed seeing that both parent classes are abstract. It doesn’t really matter in this case.

> [@plusblanc](#):
>
> If I change the database to PostgreSQL …

I suspect it will. I don’t see anything wrong with the expression that would cause it to fail, provided you’re using the most recent version of Django 5 as suggested above.  
(I’d have to try it myself to see.)

> [@plusblanc](#):
>
> If I change the database to PostgreSQL or MySQL, will what I want work?

I don’t know about MySQL. It won’t in Django unless you’re running 5.1 or apply the patch as described in this thread: [Using GeneratedField with Postgres - #2 by felixxm](https://forum.djangoproject.com/t/using-generatedfield-with-postgres/27224/2)

---

<div class="post-metadata">

**Author:** ![plusblanc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/plusblanc/32/19315_2.png) [@plusblanc](https://forum.djangoproject.com/u/plusblanc)\
**Post date:** [February 22, 2024, 1:49am UTC](https://forum.djangoproject.com/t/creating-a-model-with-generatedfield-results-in-a-doesnotexist-error/28249/6 "2024-02-22T01:49:53Z")

</div>

I’m using django version 5.0.2.

> [@sarahboyce](#):
>
> Also minor thing, max\_length should be 71 if you do first and last name with a space 👍

That’s right, I was missing this. Thank you 😊

---

<div class="post-metadata">

**Author:** ![plusblanc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/plusblanc/32/19315_2.png) [@plusblanc](https://forum.djangoproject.com/u/plusblanc)\
**Post date:** [February 22, 2024, 1:55am UTC](https://forum.djangoproject.com/t/creating-a-model-with-generatedfield-results-in-a-doesnotexist-error/28249/7 "2024-02-22T01:55:21Z")

</div>

I’ll try it with the most recent version of Django 5 and PostgreSQL, thanks for the advice!
