# Reusing a schema for addExternalRelation

**URL:** <https://community.forestadmin.com/t/reusing-a-schema-for-addexternalrelation/7300>\
**Category:** Help me!\
**Tags:** typescript, database-schema, smart-relationship\
**Created:** [July 3, 2024, 8:58am UTC](https://community.forestadmin.com/t/reusing-a-schema-for-addexternalrelation/7300 "2024-07-03T08:58:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kll](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/kll/32/7091_2.png) [@kll](https://community.forestadmin.com/u/kll)\
**Post date:** [July 3, 2024, 8:58am UTC](https://community.forestadmin.com/t/reusing-a-schema-for-addexternalrelation/7300/1 "2024-07-03T08:58:20Z")

</div>

## Feature(s) impacted

I would like to use `addExternalRelation` to build a complex one-to-many relationship.

If we were a concert app, I would like to have a relationship between users and their unclaimed concert tickets. “Unclaimed” tickets here would be a ticket with an `Email` but no `ClaimedAt` field for example. (Using `addExternalRelation` might be an XY issue here)

Tickets and users live in two different Sequelize data sources (there are two databases).

## Observed behavior

`addExternalRelation` needs a `schema` to be aware of what data is returned.

Because this is pointing to an existing collection, I would _really like_ to reuse its schema with some `Pick`ing on the fields I will project in the body.

Here is some example code:

```ts
    users.addExternalRelation('UnclaimedTickets', {
      schema: { }, // TODO plug the Pick<>ed Tickets schema somehow
      dependencies: ['...'],
      listRecords(record, context) {
        return context.dataSource.getCollection('Tickets').list(
          {
            conditionTree: { /* one giant mess */ },
          },
          [/* a few projected fields */],
        )
      },
    })

```

Is there any way to get the schema from a collection?

## Context

- Agent technology: nodejs
- Agent (forest package) name & version: 1.40.1 with sequelize 6.37.3 and sequelize-typescript 2.1.6
- Database type: MSSQL
- Recent changes made on your end if any: Migrating from old agent.

---

<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:** [July 5, 2024, 7:24am UTC](https://community.forestadmin.com/t/reusing-a-schema-for-addexternalrelation/7300/2 "2024-07-05T07:24:42Z")

</div>

Hello @kll,

Sorry for the late reply, the forum has been quite active these last few days and I’ve been cooking something for you 😉

I would not recommend the use of `addExternalRelation` in your case since the data you want to link to is present in your Forest models. The `addExternalRelation` was designed to enrich data from elsewhere, making the UI treat it as a JSONArray, you would not be able to use any of the Forest Admin UI features (relations, filter, search and so on…)

So we made a new plugin, we hope you’ll like it and find it useful [@forestadmin-experimental/plugin-filtered-relationship](https://www.npmjs.com/package/@forestadmin-experimental/plugin-filtered-relationship)🙂

This plugin allows you to create a filtered oneToMany relationship to collection present in your Forest models

Best regards,

---

<div class="post-metadata">

**Author:** ![kll](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/kll/32/7091_2.png) [@kll](https://community.forestadmin.com/u/kll)\
**Post date:** [July 5, 2024, 7:28am UTC](https://community.forestadmin.com/t/reusing-a-schema-for-addexternalrelation/7300/3 "2024-07-05T07:28:16Z")

</div>

Oooh that’s a tasty-sounding plugin, I’m checking it out immediately! 🙂

---

<div class="post-metadata">

**Author:** ![kll](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/kll/32/7091_2.png) [@kll](https://community.forestadmin.com/u/kll)\
**Post date:** [July 5, 2024, 7:49am UTC](https://community.forestadmin.com/t/reusing-a-schema-for-addexternalrelation/7300/4 "2024-07-05T07:49:26Z")

</div>

@anon7284711 Thanks for the plugin, this seems perfect! I’ve filed an issue on the repo directly 😃

---

<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:** [July 5, 2024, 8:17am UTC](https://community.forestadmin.com/t/reusing-a-schema-for-addexternalrelation/7300/5 "2024-07-05T08:17:28Z")

</div>

I’ve solved the issue and everything should be released on npm, tell me if it works out for you 😃

---

<div class="post-metadata">

**Author:** ![kll](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/kll/32/7091_2.png) [@kll](https://community.forestadmin.com/u/kll)\
**Post date:** [July 5, 2024, 9:02am UTC](https://community.forestadmin.com/t/reusing-a-schema-for-addexternalrelation/7300/6 "2024-07-05T09:02:32Z")

</div>

Thank you very much! It works perfectly 😃
