Error Sorting by Relationship Column

Feature(s) impacted

Sorting by a foreign key (relationship) column

Observed behavior

When I sort a table by a column which is a foreign key to another table, I get a Bad Request error. I believe this is because my foreign key refers to a table who’s primary key is not named “id”.

For example, if I sort my table by a column called “age_category”, which points to a table with a primay key column named “term”, I get a Bad Request error. When I go into the network tab, it looks like the Forest admin UI is making a request to the backend with the query parameter sort=-age_category.id. If I change this value to sort=-age_category.term, the request works and the page loads.

Additionally, if I edit the ForestAdmin UI URL in my browser, the frontend seems to make a correctly formatted request to the backend.

Expected behavior

The sorting works without needing to manually edit the URL.

Failure Logs

See above debugging details.

Context

  • Project name: GumboUI
  • Team name: DepMap
  • Environment name: All environments
  • Agent type & version: django-forestadmin v.1.3.2
  • Recent changes made on your end if any: None. This has been an issue for us for a while now.

Hello @Sarah_Wessel

Thank you for the well-explained issue!

I forwarded the thread to our python team: they will try to reproduce that on their test environment and come back to you shortly

1 Like

In the meantime, as a workaround, can you try setting both the reference field and sort field of the age_category collection in the layout settings?

Thank you! We can use that as a workaround for now. Although having a more long-term solution would still be really helpful. We have a lot of these relationships, and add new ones somewhat regularly, so it takes a substantial amount of time to set the reference field and sort field for all of them.

The fix is being written as we speak :slight_smile:

1 Like

Hi @Sarah_Wessel,

I’m working on the fix. It takes some more time than expected.

Hi @Sarah_Wessel,

Should be fixed in 1.4.3

Hi @anon6357957,

Unfortunately, when I upgraded to version 1.4.3, I am still seeing the same issue.

When I select a column, I am still being redirected to a URL like: https://app.forestadmin.com/GumboUI/Development/DepMap/data/model/index?sort=-age_category and receiving a 400 status code (Bad Request error) from the django backend.

Thank you for your help on this.

Sarah

After doing more testing, I found that I can patch the issue for us by forking the django-forestadmin repo and making a change like the following in the QuerysetMixin:

I don’t think that addresses the root of the issue (the id being incorrectly appended in the first place), but thought it worth mentioning in case that helps with debugging on your end.

Hi @Sarah_Wessel,

Could you check in your forestadmin-schema.json if the primary_key is set ?

No, our forestadmin-schema.json file doesn’t have a primary_key value set anywhere in the file. However, there is a reference value which contains information about the primary key. For example, the column I used as an example above is defined in the schema as:


        {
          "field": "age_category",
          "type": "Number",
          "is_filterable": true,
          "is_sortable": true,
          "is_read_only": false,
          "is_required": false,
          "is_virtual": false,
          "default_value": null,
          "integration": null,
          "reference": "model_age_category_term.term",
          "inverse_of": "model_set",
          "relationship": "BelongsTo",
          "widget": null
        },

And the table it references is defined as:


    {
      "name": "model_age_category_term",
      "is_virtual": false,
      "icon": null,
      "is_read_only": false,
      "is_searchable": true,
      "only_for_relationships": false,
      "pagination_type": "page",
      "search_fields": null,
      "actions": [],
      "segments": [],
      "fields": [
        {
          "field": "model_set",
          "type": [
            "Number"
          ],
          "is_filterable": false,
          "is_sortable": true,
          "is_read_only": false,
          "is_required": false,
          "is_virtual": false,
          "default_value": null,
          "integration": null,
          "reference": "model.model_id",
          "inverse_of": "age_category",
          "relationship": "HasMany",
          "widget": null
        },
        {
          "field": "term",
          "type": "String",
          "is_filterable": true,
          "is_sortable": true,
          "is_read_only": false,
          "is_required": false,
          "is_virtual": false,
          "default_value": null,
          "integration": null,
          "reference": null,
          "inverse_of": null,
          "relationship": null,
          "widget": null,
          "validations": [
            {
              "type": "is present",
              "message": "Ensure this value is not null or not empty"
            }
          ]
        }