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

**URL:** <https://community.forestadmin.com/t/are-sequelize-relationship-scopes-supported-with-forestadmin-datasource-sequelize/8028>\
**Category:** Help me!\
**Created:** [April 10, 2025, 9:49am UTC](https://community.forestadmin.com/t/are-sequelize-relationship-scopes-supported-with-forestadmin-datasource-sequelize/8028 "2025-04-10T09:49:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matteo](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/matteo/32/75_2.png) [@Matteo](https://community.forestadmin.com/u/Matteo)\
**Post date:** [April 10, 2025, 9:49am UTC](https://community.forestadmin.com/t/are-sequelize-relationship-scopes-supported-with-forestadmin-datasource-sequelize/8028/1 "2025-04-10T09:49:36Z")

</div>

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](https://sequelize.org/docs/v6/advanced-association-concepts/association-scopes/)).

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):

```sql
... 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:

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

```

Here is the definition of the relationship:

```js
    ...
    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`:

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

```

From `package.json`:

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

```

---

<div class="post-metadata">

**Author:** ![anon7284711](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@anon7284711](https://community.forestadmin.com/u/anon7284711)\
**Post date:** [April 10, 2025, 12:51pm UTC](https://community.forestadmin.com/t/are-sequelize-relationship-scopes-supported-with-forestadmin-datasource-sequelize/8028/2 "2025-04-10T12:51:38Z")

</div>

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:

> <https://github.com/ForestAdmin/forestadmin-experimental/blob/main/packages/plugin-filtered-relationship/README.md>

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,

---

<div class="post-metadata">

**Author:** ![Matteo](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/matteo/32/75_2.png) [@Matteo](https://community.forestadmin.com/u/Matteo)\
**Post date:** [April 14, 2025, 9:01am UTC](https://community.forestadmin.com/t/are-sequelize-relationship-scopes-supported-with-forestadmin-datasource-sequelize/8028/4 "2025-04-14T09:01:07Z")

</div>

Thank you @anon7284711

> 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.  
😇

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!! 😃  
We’ll give it a look and I’ll be back soon with a feedback.

Thank you,  
Matteo

---

<div class="post-metadata">

**Author:** ![Matteo](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/matteo/32/75_2.png) [@Matteo](https://community.forestadmin.com/u/Matteo)\
**Post date:** [April 14, 2025, 2:26pm UTC](https://community.forestadmin.com/t/are-sequelize-relationship-scopes-supported-with-forestadmin-datasource-sequelize/8028/5 "2025-04-14T14:26:24Z")

</div>

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

Thank you.  
Matteo

---

<div class="post-metadata">

**Author:** ![anon7284711](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@anon7284711](https://community.forestadmin.com/u/anon7284711)\
**Post date:** [April 14, 2025, 2:38pm UTC](https://community.forestadmin.com/t/are-sequelize-relationship-scopes-supported-with-forestadmin-datasource-sequelize/8028/6 "2025-04-14T14:38:29Z")

</div>

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,
