# Error exporting tables with no "id" column

**URL:** <https://community.forestadmin.com/t/error-exporting-tables-with-no-id-column/5026>\
**Category:** Help me!\
**Created:** [July 28, 2022, 7:48pm UTC](https://community.forestadmin.com/t/error-exporting-tables-with-no-id-column/5026 "2022-07-28T19:48:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sarah\_Wessel](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@Sarah\_Wessel](https://community.forestadmin.com/u/Sarah_Wessel)\
**Post date:** [July 28, 2022, 7:48pm UTC](https://community.forestadmin.com/t/error-exporting-tables-with-no-id-column/5026/1 "2022-07-28T19:48:25Z")

</div>

## Export to CSV Bug

## Feature(s) impacted

Export to CSV

## Observed behavior

I noticed that when exporting certain tables to CSV through the Forest Admin UI, I was getting an error (see below).

## Expected behavior

I am expecting the export to work without throwing an exception.

When I was using the Node version of the backend, this feature was working as expected. However, now that I have switched over to the Django backend, I am getting this error.

## Failure Logs

Error log:

```javascript
Traceback (most recent call last):
  File "/Users/swessel/opt/miniconda3/envs/testdbscraping/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/swessel/opt/miniconda3/envs/testdbscraping/lib/python3.7/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/swessel/opt/miniconda3/envs/testdbscraping/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/swessel/opt/miniconda3/envs/testdbscraping/lib/python3.7/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/swessel/Desktop/Repos/django-forestadmin/django_forest/resources/utils/resource.py", line 13, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/Users/swessel/opt/miniconda3/envs/testdbscraping/lib/python3.7/site-packages/django/views/generic/base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/swessel/Desktop/Repos/django-forestadmin/django_forest/resources/views/csv.py", line 36, in get
    self.fill_csv(data, writer, params)
  File "/Users/swessel/Desktop/Repos/django-forestadmin/django_forest/resources/utils/csv.py", line 27, in fill_csv
    res = self.fill_csv_relationships(res, record, data, params)
  File "/Users/swessel/Desktop/Repos/django-forestadmin/django_forest/resources/utils/csv.py", line 19, in fill_csv_relationships
    res[name] = related_res['attributes'][params[f'fields[{name}]']]
KeyError: 'id'

```

## Context

After a bit of debugging, I noticed that the csv utility assumes my table has a column named “id”, and that foreign keys in my table are pointing towards primary key columns named “id”. However, my database doesn’t always use a column named “id” as the primary key.

I created a PR on the django-forestadmin repo with a pretty simple fix: [fix(utils): prevent error in exporting tables with non-"id" primary keys by snwessel · Pull Request #110 · ForestAdmin/django-forestadmin · GitHub](https://github.com/ForestAdmin/django-forestadmin/pull/110)

- Project name: GumboUI
- Team name: DepMap
- Environment name: Production
- Agent type & version: Django
- Recent changes made on your end if any: …

---

<div class="post-metadata">

**Author:** ![anon6357957](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@anon6357957](https://community.forestadmin.com/u/anon6357957)\
**Post date:** [August 4, 2022, 7:04am UTC](https://community.forestadmin.com/t/error-exporting-tables-with-no-id-column/5026/3 "2022-08-04T07:04:26Z")

</div>

Hello @Sarah_Wessel,

Fixed in the new version of the package.

---

<div class="post-metadata">

**Author:** ![Sarah\_Wessel](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@Sarah\_Wessel](https://community.forestadmin.com/u/Sarah_Wessel)\
**Post date:** [August 10, 2022, 2:10pm UTC](https://community.forestadmin.com/t/error-exporting-tables-with-no-id-column/5026/4 "2022-08-10T14:10:51Z")

</div>

Hello @anon6357957,

Thank you for taking a look at it! Unfortunately, I’m still getting similar errors after using the new version of the package.

However, I created a new PR with a couple of very small changes (building off of the changes you made) which fix the issue for us entirely:

> <https://github.com/ForestAdmin/django-forestadmin/pull/114>
>
> \## Definition of Done
> The model's pk should be able to have a name other than '…id'. 
> 
> There were some changes made related to this 8 days ago (https://github.com/ForestAdmin/django-forestadmin/pull/112) but it did not fix the issue for us (\[community forum post here\](https://community.forestadmin.com/t/error-exporting-tables-with-no-id-column/5026)). 
> 
> I would like to propose a couple more small changes which get it working for our use case. 
> 
> \### General
> 
> \- \[x\] Write an explicit title for the Pull Request, following \[Conventional Commits specification\](https://www.conventionalcommits.org)
> \- \[x\] Test manually the implemented changes
> \- \[x\] Validate the code quality (indentation, syntax, style, simplicity, readability)
> 
> \### Security
> 
> \- \[x\] Consider the security impact of the changes made

---

<div class="post-metadata">

**Author:** ![anon6357957](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@anon6357957](https://community.forestadmin.com/u/anon6357957)\
**Post date:** [August 11, 2022, 7:13am UTC](https://community.forestadmin.com/t/error-exporting-tables-with-no-id-column/5026/5 "2022-08-11T07:13:55Z")

</div>

Hello @Sarah_Wessel ,

Could you show me your model that raise this error ?

thanks

---

<div class="post-metadata">

**Author:** ![Sarah\_Wessel](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@Sarah\_Wessel](https://community.forestadmin.com/u/Sarah_Wessel)\
**Post date:** [August 16, 2022, 2:31pm UTC](https://community.forestadmin.com/t/error-exporting-tables-with-no-id-column/5026/6 "2022-08-16T14:31:59Z")

</div>

Hi @anon6357957, sorry for the delay in getting back to you! Here’s a simplified version of the django models I’m using that should recreate the error for you:

```javascript
from django.db import models

class Model(models.Model):
    model_id = models.TextField(primary_key=True)
    patient_id = models.TextField(blank=True, null=True)
    growth_pattern = models.ForeignKey(
        "ModelGrowthPatternTerm",
        models.DO_NOTHING,
        db_column="growth_pattern",
        blank=True,
        null=True,
    )

    class Meta:
        db_table = "model"

class ModelGrowthPatternTerm(models.Model):
    term = models.TextField(primary_key=True)

    class Meta:
        db_table = "model_growth_pattern_term"

```

Thank you again for the help, and let me know if there’s any other info you need!

---

<div class="post-metadata">

**Author:** ![anon6357957](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@anon6357957](https://community.forestadmin.com/u/anon6357957)\
**Post date:** [August 16, 2022, 3:09pm UTC](https://community.forestadmin.com/t/error-exporting-tables-with-no-id-column/5026/7 "2022-08-16T15:09:02Z")

</div>

Thanks @Sarah_Wessel,  
I’m going to test with your models.
