Duplicate fields when the primary key is also a foreign key

Feature(s) impacted

In Forest Admin UI

  • Extra fields + misplaced fields when creating new object
  • Relationships are not properly linked in the “related data” column.

Schema

                       Table "codes"
   Column   |           Type           | Collation | Nullable | Default 
------------+--------------------------+-----------+----------+---------
 created_at | timestamp with time zone |           |          | now()
 code       | character varying        |           | not null | 
 ...        | character varying        |           | not null | 

Indexes:
    "codes_pkey" PRIMARY KEY, btree (code)
Referenced by:
    TABLE "A" CONSTRAINT "A_code_fkey" FOREIGN KEY (code) REFERENCES codes(code) ON UPDATE CASCADE
    TABLE "B" CONSTRAINT "B_code_fkey" FOREIGN KEY (code) REFERENCES codes(code) ON UPDATE CASCADE

                              Table "A"
          Column          |           Type           | Collation | Nullable | Default 
--------------------------+--------------------------+-----------+----------+---------
 created_at               | timestamp with time zone |           |          | now()
 code                     | character varying        |           | not null | 
 ...                      | character varying        |           | not null | 

Indexes:
    "A_pkey" PRIMARY KEY, btree (code)
Foreign-key constraints:
    "A_code_fkey" FOREIGN KEY (code) REFERENCES codes(code) ON UPDATE CASCADE

Observed behavior

When creating a new record in a table A (primary key = foreign key (primary key of codes) = ‘code’):

Problem 1:
When the user creates a new row in the table “A”, there are two fields for code:

  • One that is free form (I imagine because it’s the primary key)
  • One that is Code through code * (link to an existing code through code)

Problem 2:
When the user creates a new row in the table “codes”, there is an extra field: A: Link to an existing A

Expected behavior

Problem 1:
There is only one field code that is a scrollable / lookahead field.

Problem 2:
This field should not appear

Context

  • Project name: BDD DS
  • Team name: message me
  • Environment name: prod
  • Database type: postgres

Hi @cmeng and welcome to our community :slight_smile:

In order to help you I would need the two collections and the real field names that contains the foreign keys and the primary keys.

Best regards,

Shohan

Hi Shohan,

All the information I put is correct, but the table “A” is the table items. I believe that is all the information you need. Let me know if you need anything else.

Cheers,
cmeng

Hi @cmeng ,

I cannot reproduce your issue, or maybe I don’t understand it very well.
Forest admin creates a new field for each relationship in your DB, representing the record of the other table. In you case, the foreign key is also the primary key so it remains in the layout.

From what I understand from your schema, when you create a new “A”, you should have to provide either “code” and type it manually, or “code through code” and select it from the typeahead widget.
It seems like just a layout issue: you need to hide unwanted fields and disable readonly for the field you want to be able to edit manually.

Please let me know if it solved you issue.