get the record in smart field in a relational table
Context
we have two tables that have many-to-many relationship
let’s call them tasks, and rewards
both rewards, and tasks has id, and title fields
to connect tasks and rewards we have a pivot table called rewards_tasks which has a reward_id, and task_id
in the rewards_tasks collection I’m trying to add a smart field to show the title of rewards, and the title the of tasks instead of their id, but when I try to add a smart field, it doesn’t show the value, it doesn’t even try to fetch the record to get the title from
this code works on other collections that are not relational, but it’s not working on this one
is there any other way to get the title from the related entities?
There should be no issue with what you’re trying to achieve.
I just tried on my side and I didn’t face any issue.
Are you able to see the Task Title field in the UI?
When it is displayed, can you see the logs in your server?
The record you are displaying (a reward_task) should be able to access to a reward (or at least a reward_id, so that you can fetch the reward in that case) and so access to the reward title.
Updating your Sequelize model to make it the primary key should then fix the issue I guess.
We do support composite primary key, but they are know to be the source of potential issues.
Eventually, could you try to add the id column as the primary key, and remove it from the giveawayId & taskId in Sequelize?
// This model was generated by Forest CLI. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/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/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const GiveawayTasks = sequelize.define(
'giveawayTasks',
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: Sequelize.literal('uuid_generate_v4()'),
allowNull: false,
},
},
{
tableName: 'giveaway_tasks',
schema: process.env.DATABASE_SCHEMA,
},
);
return GiveawayTasks;
};
Mhh, weird, I can’t see any new .forestadmin-schema.json sent on your project. Did you restart your development server?
On the other end, maybe you could set the Reference field (Available in your collection settings) to replace Task and Reward always to their title (See our documentation) would suit your need ?
the issue was on te both sides of the relationship
so I had to first add id to pivot table
then use correct methods for relationships on two ends of the pivot table
by regenerating the models it worked.