Error exporting tables with no "id" column

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:

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

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

Hello @Sarah_Wessel,

Fixed in the new version of the package.

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:

Hello @Sarah_Wessel ,

Could you show me your model that raise this error ?

thanks

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:

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!

1 Like

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