# Django 4.2, is a 2nd autofield-like field on a model possible?

**URL:** <https://forum.djangoproject.com/t/django-4-2-is-a-2nd-autofield-like-field-on-a-model-possible/28278>\
**Category:** Using the ORM\
**Created:** [February 20, 2024, 9:18pm UTC](https://forum.djangoproject.com/t/django-4-2-is-a-2nd-autofield-like-field-on-a-model-possible/28278 "2024-02-20T21:18:59Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![jaddison](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jaddison/32/19332_2.png) [@jaddison](https://forum.djangoproject.com/u/jaddison)\
**Post date:** [February 21, 2024, 12:47am UTC](https://forum.djangoproject.com/t/django-4-2-is-a-2nd-autofield-like-field-on-a-model-possible/28278/4 "2024-02-21T00:47:28Z")

</div>

@charettes thank you - that was the breadcrumb I needed. The following rough implementation works:

```python
class Default(Expression):
    def as_sql(self, compiler, connection):
        return "DEFAULT", []

class AutoPositiveBigIntegerField(models.PositiveBigIntegerField):
    @property
    def db_returning(self):
        return True

    def get_default(self):
        return Default()

class Invoice(models.Model):
    ...
    number = AutoPositiveBigIntegerField(unique=True)

```

The above, coupled with the following manual migration on the `number` field:

```python
from django.db import migrations

class Migration(migrations.Migration):

    dependencies = [
        ("billing", "0003_my_migration"),
    ]

    operations = [
        migrations.RunSQL(
            "ALTER TABLE invoice ALTER COLUMN number ADD GENERATED BY DEFAULT AS IDENTITY;",
            reverse_sql="ALTER TABLE invoice ALTER COLUMN number DROP IDENTITY IF EXISTS;"
        ),
    ]

```

---

_[View the full topic](https://forum.djangoproject.com/t/django-4-2-is-a-2nd-autofield-like-field-on-a-model-possible/28278)._
