Problem on duplication button

Hello there !
I’m working on forest integration in my company and we have a realy realy bad database.We have some Id as primary key but we dont use it as foreign key.
Exemple : I have some user (id, uniqueId, name,…) and some invoicing (id, price,…)
One invoicing belongsTo one linked by user.uniqueId.The probleme here is when i want to duplicate my invoicing, Forest use the primary id of user for the relation into the duplicate one.How can i change that ? I’ll try the Change a collection’s reference field of yoru documentation but it seems to only change the appareance of my linked table on the view.

This is my sequelize model :

// This model was generated by Lumber. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v5/reference-guide/models/enrich-your-models
module.exports = (sequelize, DataTypes) => {
  const { Sequelize } = sequelize;
  // This section contains the fields of your model, mapped to your table's columns.
  // Learn more here: https://docs.forestadmin.com/documentation/v/v5/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
  const Invoicing = sequelize.define('invoicing', {
    id: {
      type: DataTypes.INTEGER,
      field: 'Id',
      primaryKey: true,
      autoIncrement: true
    },
    userId: {
      type: DataTypes.STRING,
    },
    requestSource: {
      type: DataTypes.STRING,
    },
    type: {
      type: DataTypes.STRING,
    },
    feePercentageHt: {
      type: DataTypes.DOUBLE,
      field: 'feePercentageHT',
    },
    feePercentageTtc: {
      type: DataTypes.DOUBLE,
      field: 'feePercentageTTC',
    },
    feePercentagePrepaid: {
      type: DataTypes.STRING,
    },
    feeFlatHt: {
      type: DataTypes.DOUBLE,
      field: 'feeFlatHT',
    },
    feeFlatTtc: {
      type: DataTypes.DOUBLE,
      field: 'feeFlatTTC',
    },
    feeFlatPrepaid: {
      type: DataTypes.STRING,
    },
    clientFeeHt: {
      type: DataTypes.DOUBLE,
      field: 'clientFeeHT',
    },
    clientFeeTtc: {
      type: DataTypes.DOUBLE,
      field: 'clientFeeTTC',
    },
    clientFeePercentHt: {
      type: DataTypes.DOUBLE,
      field: 'clientFeePercentHT',
    },
    clientFeePercentTtc: {
      type: DataTypes.DOUBLE,
      field: 'clientFeePercentTTC',
    },
    clientFeePrepaid: {
      type: DataTypes.STRING,
    },
    debtRecovering: {
      type: DataTypes.BOOLEAN,
      defaultValue: Sequelize.literal('true'),
    },
    debtRecoveryFeeHt: {
      type: DataTypes.DOUBLE,
      field: 'debtRecoveryFeeHT',
    },
    debtRecoveryFeeTtc: {
      type: DataTypes.DOUBLE,
      field: 'debtRecoveryFeeTTC',
    },
    requestLinkId: {
      type: DataTypes.STRING,
    },
    integration: {
      type: DataTypes.STRING,
    },
    createdAt: {
      type: DataTypes.DATE,
      defaultValue: Sequelize.literal('(getdate())'),
    },
  }, {
    tableName: 'invoicing',
    timestamps: false,
  });
  // This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/v/v5/reference-guide/relationships#adding-relationships.
  Invoicing.associate = (models) => {
    Invoicing.belongsTo(models.users,  {
      foreignKey: 'userId',
      targetKey: 'uniqueId',
      keyType: DataTypes.STRING,
    });
  };
  return Invoicing;
};

My userId and uniqueId are provided by my applicaiton, and we have some primary ID generated by MSSQL.
But all my FK refferecies are linker to another column uniqueID.
For this exemple :

user.id = mssql autoincrement
user.uniqueID = my application ID

and into my invoicing table invoicing.userId are filled with user.uniqueId.
The struggle is when i duplicate an entry with Forest, invoicing.userId are filled with user.userId generated by MSSQL. Wich is not good for me.

how can i fix that ?
thx in advance

Hi @Dylan_Lambert,

Welcome to the Forest Admin community!

Let me investigate this today.

After having dug into this, it looks like Forest Admin does not support at all (not only duplicate actions, but also record creation and update) associations that target a column that is not a primary key.

Maybe a workaround in your case, would be to deactivate the “native” belongsTo declaration in you invoice model to let appear the raw userId field (that should duplicate properly).
And then define a Smart BelongsTo Relationship if you need to to ease the navigation from one invoice to its user.

Does it make sense?

Hi.

Thx for your time, yes i understand the workaround for solving this issue.

Have a nice day !