Are sequelize relationship scopes supported with `@forestadmin/datasource-sequelize`?

Hi Forest!
We’re dealing with a weird behaviour…

We have a sequelize model with a relationship.
In the relationship we have a scope (see Association Scopes | Sequelize).

When we “use” this relationship through sequelize, in the underlying sql query we can see the scope implemented correctly in the WHERE clause (indented for reading):

... WHERE (
  "MyModel"."deleted_at" IS NULL 
  AND (
    NOT ("MyModel"."id_fk" IS NULL) 
      AND "MyModel"."id_fk" IS NULL)
    )
  )

When the very same relationship is “used” by forest, then the scope is ignored. Here is the WHERE clause in this case:

WHERE (
  "MyModel"."deleted_at" IS NULL 
  AND "MyModel"."id_fk" IS NULL
)

Here is the definition of the relationship:

    ...
    hasMany(models.MyModel, {
      as: 'myAliasName',
      foreignKey: ...
      sourceKey: ...
      scope: {
        [Op.not]: {
          idFk: null,
        },
      },
    })
    ...

Therefore, my question is: are the sequalize relationship scopes supported in @forestadmin/datasource-sequelize and @forestadmin/agent?
As far as I can remember, they used to work in forest-express-sequelize

Thank you in advance for a check,
Matteo


Our versions

From .forestadmin-schema.json:

  "meta": {
    "liana": "forest-nodejs-agent",
    "liana_version": "1.60.1",
    "liana_features": null,
    "stack": {"engine": "nodejs", "engine_version": "20.12.2"}
  }

From package.json:

...
    "@forestadmin/agent": "^1.60.1",
    "@forestadmin/datasource-sequelize": "^1.12.1",
    "sequelize": "^6.28.0",
...

Hello @Matteo,

I am pretty sure that association scopes were never supported on v1 and they are currently not supported on v2 either.

As a workaround I could suggest you to use a plugin to create a filtered OneToMany relationship. As detailed in the following doc:

I do understand that this is not ideal as you would have to redefine the relationship in your agent, I’ll forward your feedback to the product team.

Best regards,

1 Like

Thank you @dogan.ay

I am pretty sure that association scopes were never supported on v1 and they are currently not supported on v2 either.

If that’s the case, then I guess I have to admit that maybe I’m the one remembering it wrong.
:innocent:

Thank you for the hint about the experimental feature. You’re right, I’m not happy to redefine the relationship twice, but if there’s no alternative… well… we’ll make a virtue of necessity!! :smiley:
We’ll give it a look and I’ll be back soon with a feedback.

Thank you,
Matteo

1 Like

@dogan.ay we’ll go with the “filtered” smart relationship, as you suggested.

Thank you.
Matteo

1 Like

Glad that I could help unlock your situation, I have forwarded the feedback to the product team, thanks for sharing and coming to the community support.

Best regards,