One to one relation on the same entity

Feature(s) impacted

Hi, I have an entity called Asset, which has. a one-to-one relation another Asset, through the field thumbnail and the FK thumbnailId, using TypeORM it looks like this:

@Entity()
export class Asset {
  @Column({ type: 'int', nullable: true })
  thumbnailId: number | null

  @OneToOne(() => Asset)
  @JoinColumn({ name: 'thumbnailId' })
  readonly thumbnail?: Asset | null
}

I have created the model for forest and added the association for the thumbnail field with:

    Asset.hasOne(models.asset, {
      foreignKey: {
        name: 'thumbnailId',
        field: 'thumbnailId',
      },
      as: 'thumbnail',
    })

Observed behavior

The problem is that when creating new thumbnail from the Asset parent on forest, it’s attaching automatically the thumbnailId also to the child thumbnail (Asset), but I want that the association of thumbnailId will be only on the parent Asset only and not on the child Asset (which is the thumbnail)

Parent Asset: {
   id: 1,
   thumbnailId: 10
}

Child Asset: {
   id: 10,
   thumbnailId: 1 // the association that happens on forest which I don't want
}

Expected behavior

When creating new child Asset from a parent Asset on one-to-one relation, the child Asset thumbnailId should be null, only the thumbnailId on the parent should have value

Parent Asset: {
   id: 1,
   thumbnailId: 10 // only parent entity need to have an association to the child Asset
}

Child Asset: {
   id: 10,
   thumbnailId: null
}

Context

  • Agent (forest package) name & version: “forest-express-sequelize”: “^9.2.9”, “forest-cli”: “^4.1.3”, (nodejs )“engine_version”: “18.16.0”
  • Database type: Postgres

Hello @Sagi,

Instead of declaring a hasOne relationship, could you try declaring a belongsTo relationship?

Tried with belongsTo and got same result

Hello,

I reproduced your use case locally:

First, here is my declaration in Sequelize:

  Asset.init(
      {
        id: {
          primaryKey: true,
          type: DataTypes.INTEGER,
          autoIncrement: true,
        },
        url: {
          type: DataTypes.TEXT,
        },
      },
      {
        sequelize,
        tableName: "assets",
        paranoid: false,
        createdAt: false,
        updatedAt: false,
      }
    );

  Asset.belongsTo(Asset, {
    foreignKey: "thumbnailId",
    as: "thumbnail",
  });

  Asset.hasOne(Asset, {
    foreignKey: "thumbnailId",
    as: "parent",
  });

When creating an asset in Forest Admin, here is what is displayed once I’ve entered an URL for this main asset:

Then I click on “Thumbnail → Create a new Thumbnail”

Then the same screen appears, here I just need to edit the url and click on Create

Going back on the creation screen on the main asset, it fills in the thumbnail id:

After the whole process, I have 2 different assets:

  1. My main asset having the field “thumbnail” showing the ID of the corresponding thumbnail)
  2. My thumbnail having the field “parent” showing the id of the main asset

In DB:

Capture d’écran 2023-11-13 à 14.31.28

Thank you so much for your help! It’s working