Same column as primary key and foreign key

Feature(s) impacted

Relationship ID propagation in Forest Admin UI when the relationship uses the same column as both PK and FK.

Observed behavior

When creating a new record in a child table that has a relationship with a parent table (sharing the same ID):

  1. User creates new child record from the parent record details in edit mode (clicking “+”)
  2. On the child create page, linked id is correctly pre-filled
  3. But the ID field remains blank and required, without any autocomplete
  4. So the user has to copy the ID from the product (despite the linked id being already filled)
  5. This can be cumbersome, especially when the reference of the parent is something else than the id since the user has to come back etc

Expected behavior

When selecting the parent record in the relationship field, the ID field should automatically populate with the parent record’s UUID since they share the same ID in a one-to-one relationship. Or the ID field should not be visible since it’s essentially duplicated with the linked id field.

Context

Database schema:

CREATE TABLE parent (
  id UUID PRIMARY KEY,
  -- other fields
);

CREATE TABLE child (
  id UUID PRIMARY KEY,
  variant TEXT NOT NULL,
  FOREIGN KEY (id) REFERENCES parent(id)
);

Model definitions:

// parent model
const Parent = sequelize.define('parent', {
  id: {
    type: DataTypes.UUID,
    primaryKey: true,
    defaultValue: Sequelize.literal('uuid_generate_v4()')
  }
});

// child model
const Child = sequelize.define('child', {
  id: {
    type: DataTypes.UUID,
    primaryKey: true,
    allowNull: false
  },
  variant: {
    type: DataTypes.TEXT,
    allowNull: false
  }
});

// Relationship
Child.belongsTo(Parent, {
  foreignKey: {
    name: 'idKey',
    field: 'id'
  },
  as: 'linkedId'
});
  • Project name: contact me
  • Team name: contact me
  • Environment name: Development / Staging / Production

{
“liana”: “forest-express-sequelize”,
“liana_version”: “9.4.1”,
“stack”: {
“database_type”: “postgres”,
“engine”: “nodejs”,
“engine_version”: “20.9.0”,
“orm_version”: “6.37.3”
}
}

Hey @louisl,

Thank you for your detailed report.
Could you please share a video reproducing the issue with the web console open ?

Hello, just sent you a video, thanks

Okey I do not reproduce your issue.
Because I think it’s more a misconfiguration :grin:

You have 2 times the same field (one for the id one for the relationship).
What you should do is hide the “Product Id” field during the creation at least or make it read only and I’m pretty sure it will fix your issue :crossed_fingers:

1 Like

Hello, thanks!

Read-only makes the trick but that’s a bit unexpected (this comes straight from forest schema:update), or at least it should be documented somewhere? Ideally that would be done automatically?

I’ll report it to the team :slight_smile:

1 Like