ForestAdmin / forest-express-sequelize

🌱 Express/Sequelize agent for Forest Admin to integrate directly to your existing Express/Sequelize backend application.
https://www.forestadmin.com
GNU General Public License v3.0
190 stars 48 forks source link

"Create a new [entity]" sets the new entity to all the relations of kind [entity] #520

Closed Minishlink closed 4 years ago

Minishlink commented 4 years ago

Expected behavior

On a table A that has 2 relations X and Y with the same table B, when you edit a row of table A without X nor Y, if you create a new X, save the new X, and save the row of table A, the row will have only X set to the newly created B row.

Actual behavior

On a table A that has 2 relations X and Y with the same table B, when you edit a row of table A without X nor Y, if you create a new X, save the new X, and save the row of table A, the row will have X and Y set to the newly created B row.

Failure Logs

No failure per se

Context

Contact me for access to the ForestAdmin project.

Manager.belongsTo(models.student, {
  foreignKey: {
    name: 'studentUserIdKey',
    field: 'studentUserId',
  },
  target: {
    name: 'userId',
  },
  as: 'studentUser',
});
Manager.hasMany(models.student, {
  foreignKey: {
    name: 'managerIdKey',
    field: 'managerId',
  },
  as: 'students',
});
Manager.belongsTo(models.student, {
  foreignKey: {
    name: 'studentUserIdKey',
    field: 'studentUserId',
  },
  target: {
    name: 'userId',
  },
  as: 'studentUser',
});
Manager.hasMany(models.student, {
  foreignKey: {
    name: 'managerIdKey',
    field: 'managerId',
  },
  as: 'students',
});

Upgraded to latest versions and still reproducing it:

Was on:

Also reproduces on another project with:

Workaround is to remove X in table A and remove Y in table B, so that you can still set X and Y, but on different tables. It's a real pain for the user though, particularly because ForestAdmin doesn't support join relations when searching foreign entities. The user has to go to table A, search for the wanted row, copy the ID, then go to table B and set the relation by searching for the ID...

Minishlink commented 4 years ago

I might have forgotten to restart the server, looks like I cannot reproduce it 🤔 Will reopen with more info if I stumble upon it again.