# Django query auto add 'id' during query process then it error

**URL:** <https://forum.djangoproject.com/t/django-query-auto-add-id-during-query-process-then-it-error/31756>\
**Category:** Using the ORM\
**Created:** [June 5, 2024, 9:38am UTC](https://forum.djangoproject.com/t/django-query-auto-add-id-during-query-process-then-it-error/31756 "2024-06-05T09:38:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnyb1509](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/johnnyb1509/32/21294_2.png) [@johnnyb1509](https://forum.djangoproject.com/u/johnnyb1509)\
**Post date:** [June 5, 2024, 9:38am UTC](https://forum.djangoproject.com/t/django-query-auto-add-id-during-query-process-then-it-error/31756/1 "2024-06-05T09:38:30Z")

</div>

Hi,

Today, I have problem when query data from defined model, given that my model does not contain any ‘id’ columns

```auto
# models.py
class Omo(models. Model):
    id_date = models.IntegerField()
    date = models.DateField()
    transactionmode = models.CharField(db_column='transactionMode', max_length=50, db_collation='SQL_Latin1_General_CP1_CI_AS', blank=True, null=True) # Field name made lowercase.
    omo_rate = models.FloatField(blank=True, null=True)
    acceptedvalue_bil = models.FloatField(db_column='acceptedValue_bil', blank=True, null=True) # Field name made lowercase.
    term = models.FloatField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'omo_rate'

```

Then, in `views.py`

```auto
class AccessDatabase(CreateView):
      def form_valid(self, form, **kwargs):
            cleaned_data = form.cleaned_data
            dateRange = cleaned_data['dateRange']
            dataObject = model_class.objects.using("databaseName").filter(date__range=[dateRange[0], dateRange[1]]).all().order_by("-id_date").values()

```

It returns the error:

```auto
django.db.utils.ProgrammingError: ('42S22', "[42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'id'. (207) (SQLExecDirectW)")

```

And I try to find the root cause and find out it automatically add the ‘id’ column to the query. See the picture below  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/7/b/7b8e4d1133846d299972148d5d5ae3d165d2633e.png)

How to solve this problem?

---

<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:** [June 5, 2024, 11:33am UTC](https://forum.djangoproject.com/t/django-query-auto-add-id-during-query-process-then-it-error/31756/2 "2024-06-05T11:33:54Z")

</div>

Django requires that every model have a primary key. If there isn’t one defined in the model, Django will add it. See [Models | Django documentation | Django](https://docs.djangoproject.com/en/5.0/topics/db/models/#automatic-primary-key-fields)

---

<div class="post-metadata">

**Author:** ![johnnyb1509](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/johnnyb1509/32/21294_2.png) [@johnnyb1509](https://forum.djangoproject.com/u/johnnyb1509)\
**Post date:** [June 11, 2024, 8:37am UTC](https://forum.djangoproject.com/t/django-query-auto-add-id-during-query-process-then-it-error/31756/3 "2024-06-11T08:37:04Z")

</div>

Thank you so much, master!
