Duplicate entries when filtering for ID

Hi,

I have a collection where some entires are shown as duplicates
Even when I filter by ID the result has 2 entries (See attached screenshot).
Since the ID is the primary key it has a unique constraint. I even checked the database but there is only one entry.

I’m using Postgres with forest-express-sequelize version 8.5.4

It also seems that this affect some smart actions where I use RecordsGetter and expect a single entity

Hey @bef :wave:

Was the list route for this collection override? Is that a recent issue or did you upgrade your forest-express-sequelize version recently?

Thanks in advance

Thanks for the reply.
In the list route for this collection the request.query.filters are changed in some cases. But when filtering for id the request.query.filters stays the same.
We upgraded the forest-express-sequelize to check if the issue is gone, but we had this issue before and it is still there.

I’m currently unable to reproduce the issue :confused:
Could you please share (Here or via DM) the associated route override?

It could really help understand what is happening here :pray:

I sent you a DM with the associated route override.

I found the reason. The problem is that there is a relation to another table which is defined as a one-to-one relation.
The relation does not use the ids but other values to map to each other as described here

    ProductKey.belongsTo(models.otherItem, {
      foreignKey: 'value',
      targetKey: 'otherItemValue',
    });

The problem is, that it really is a one-to-many relationship, because there could be mane otherItem not only one. This results in as many search results as otherItems are available with this relation.

How can I change this to a one-to-many relation?

Hey @bef,

I’m not sure to fully understand here.

How can I change this to a one-to-many relation?

I would suggest to read the Sequelize documentation. This part can be especially useful if you want to define your one-to-many relation.

Let me know if that helps

I try to clarify my question.
The “product key” has many “other items”, so I would define the relation like this:

ProductKey.hasMany(models. otherItem, {
      foreignKey: {
        name: 'otherItemValue',
        field: 'other_item_value',
      },
      as: 'otherItems',
    });

But this always uses the id of productKey for the relation.
How can I tell that it should uses another column as foreign key?

You should be able to specify sourceKey and targetKey (Documentation) when defining your hasMany relation.

Is that what you are looking for ?

1 Like

Thank you very much. That is what I was looking for :slight_smile:

1 Like