# Can we make a relation between two tables that are in different databases

**URL:** <https://community.forestadmin.com/t/can-we-make-a-relation-between-two-tables-that-are-in-different-databases/5497>\
**Category:** Help me!\
**Tags:** howtos, dashboard\
**Created:** [November 7, 2022, 9:01am UTC](https://community.forestadmin.com/t/can-we-make-a-relation-between-two-tables-that-are-in-different-databases/5497 "2022-11-07T09:01:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacques\_liao](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/jacques_liao/32/3723_2.png) [@jacques\_liao](https://community.forestadmin.com/u/jacques_liao)\
**Post date:** [November 7, 2022, 9:01am UTC](https://community.forestadmin.com/t/can-we-make-a-relation-between-two-tables-that-are-in-different-databases/5497/1 "2022-11-07T09:01:46Z")

</div>

Hello,

The goal is to set up a **Related Data** in one of the tables.  
Is it possible to make a link between two tables that are in two **different databases**?  
If not, is there any other solution to make the link between these two tables?

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![jacques\_liao](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/jacques_liao/32/3723_2.png) [@jacques\_liao](https://community.forestadmin.com/u/jacques_liao)\
**Post date:** [November 7, 2022, 1:30pm UTC](https://community.forestadmin.com/t/can-we-make-a-relation-between-two-tables-that-are-in-different-databases/5497/2 "2022-11-07T13:30:07Z")

</div>

I tried to create a relationship between the **beneficiary** table and the **refundRequest** table where the **beneficiary** table has multiple **refundRequests**.

**refundRequest** file in the forest folder

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/b/b86fa4589b2c99fc2c0394085eff94cae90bf951.png)

**beneficiary** file in the forest folder

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/b/bf5bb6a525109a929d488ece1e30d2d9f8d3c26c.png)

And when I go to the dashboard I get this

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/7/7bd940b14b87185dd8fbc8a64fb1a2f32a48e5d6.png)  
but in the logs I don’t see any error  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/9/946b9fccf147ebf8550a50df13c1d84baa4750e3.png)

So I thought that since the two tables are in different databases, the following error should occur.  
Is there any other way to get around this?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Alban\_Bertolini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/alban_bertolini/32/1077_2.png) [@Alban\_Bertolini](https://community.forestadmin.com/u/Alban_Bertolini)\
**Post date:** [November 7, 2022, 2:49pm UTC](https://community.forestadmin.com/t/can-we-make-a-relation-between-two-tables-that-are-in-different-databases/5497/3 "2022-11-07T14:49:14Z")

</div>

Hello,  
We have a new [JS/TS agent](https://github.com/ForestAdmin/agent-nodejs) which allows you to do this in [one line](https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/relationships/multiple-records).

For your problem, do you have any error on the frontend ?  
You should use a [smart has many relationship](https://docs.forestadmin.com/documentation/reference-guide/models/relationships/create-a-smart-relationship#creating-a-hasmany-smart-relationship) not a belongs to.

---

<div class="post-metadata">

**Author:** ![jacques\_liao](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/jacques_liao/32/3723_2.png) [@jacques\_liao](https://community.forestadmin.com/u/jacques_liao)\
**Post date:** [November 9, 2022, 10:06am UTC](https://community.forestadmin.com/t/can-we-make-a-relation-between-two-tables-that-are-in-different-databases/5497/4 "2022-11-09T10:06:53Z")

</div>

Hello @Alban_Bertolini  
So I could try to make a hasMany relationship but I have the following error

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/a/af0dd3fafa327f18cdfb81e21ba2e1b0f41317b0.png)

I think I have this error because my two tables are not in the same database, one is in a database named Gateway and the other NP

And if not, concerning the new JS agent, which file should I put them in? forest, model, route or app.js file?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Alban\_Bertolini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/alban_bertolini/32/1077_2.png) [@Alban\_Bertolini](https://community.forestadmin.com/u/Alban_Bertolini)\
**Post date:** [November 9, 2022, 10:14am UTC](https://community.forestadmin.com/t/can-we-make-a-relation-between-two-tables-that-are-in-different-databases/5497/5 "2022-11-09T10:14:25Z")

</div>

Hello 🙂  
Could you send your code please ? You should make a first request to get your beneficiary and a second to get all the refundRequests related to the beneficiary. You can’t make one request because the collections are not in the same database. I think it should work but in any case you will not be able to write from the relation.  
Also if you want to migrate to agentV2, you should use sequelize datasource and migrate all your models, smart actions, smart fields etc. by recoding them.

---

<div class="post-metadata">

**Author:** ![jacques\_liao](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/jacques_liao/32/3723_2.png) [@jacques\_liao](https://community.forestadmin.com/u/jacques_liao)\
**Post date:** [November 9, 2022, 12:05pm UTC](https://community.forestadmin.com/t/can-we-make-a-relation-between-two-tables-that-are-in-different-databases/5497/6 "2022-11-09T12:05:29Z")

</div>

Here is the code found in our routes folder **/routes/main/beneficiaries**

```javascript
router.get('/beneficiaries/:beneficiary_id/relationships/refundRequests', (req, res, next) => {
  const beneficiaryId = req.params.beneficiary_id;
  const limit = parseInt(req.query.page.size, 10) || 20;
  const offset = (parseInt(req.query.page.number, 10) - 1) * limit;
  const include = [{
    model: beneficiaries,
    as: 'beneficiaries',
    where: { fkBeneficiaryId: beneficiaryId },
  }];

  // find the refundRequests for the requested page and page size
  const findAll = refundRequests.findAll({
    include,
    offset,
    limit,
  });

  // count all refundRequests for pagination
  const count = refundRequests.count({ include });

  // resolve the two promises and serialize the response
  const serializer = new RecordSerializer(refundRequests);
  Promise.all([findAll, count])
    .then(([refundRequestsFound, refundRequestsCount]) =>
      serializer.serialize(refundRequestsFound, { count: refundRequestsCount }))
    .then((recordsSerialized) => res.send(recordsSerialized))
    .catch(next);
});

```

the other piece of code in **/forest/main/beneficiary**

```javascript
    {
      field: 'refundRequests',
      type: ['String'],
      reference: 'refundRequests.fkBeneficiaryId'
    }

```

---

<div class="post-metadata">

**Author:** ![Alban\_Bertolini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/alban_bertolini/32/1077_2.png) [@Alban\_Bertolini](https://community.forestadmin.com/u/Alban_Bertolini)\
**Post date:** [November 9, 2022, 2:47pm UTC](https://community.forestadmin.com/t/can-we-make-a-relation-between-two-tables-that-are-in-different-databases/5497/7 "2022-11-09T14:47:03Z")

</div>

🤕 Could you add a break point or a “console.log” to be sure which forest calls this end point please ? 🙏

---

<div class="post-metadata">

**Author:** ![jacques\_liao](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/jacques_liao/32/3723_2.png) [@jacques\_liao](https://community.forestadmin.com/u/jacques_liao)\
**Post date:** [November 9, 2022, 3:55pm UTC](https://community.forestadmin.com/t/can-we-make-a-relation-between-two-tables-that-are-in-different-databases/5497/8 "2022-11-09T15:55:03Z")

</div>

I put this console in my code

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/3/3702c105d1ccfae6aa3e361a0a1525a147682cd2.png)

and I can find it in my server logs

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/9/92a7114281a4e72121297e54656bcc407b0874a5.png)

---

<div class="post-metadata">

**Author:** ![Alban\_Bertolini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/alban_bertolini/32/1077_2.png) [@Alban\_Bertolini](https://community.forestadmin.com/u/Alban_Bertolini)\
**Post date:** [November 9, 2022, 4:11pm UTC](https://community.forestadmin.com/t/can-we-make-a-relation-between-two-tables-that-are-in-different-databases/5497/9 "2022-11-09T16:11:44Z")

</div>

Instead to do an “include”, could you use a where condition because the “include” will do a join between “beneficiaries” and “refundRequests”.

```javascript
refundRequests.findAll({
  where: { fkBeneficiaryId: beneficiaryId },
});

```

---

<div class="post-metadata">

**Author:** ![jacques\_liao](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/jacques_liao/32/3723_2.png) [@jacques\_liao](https://community.forestadmin.com/u/jacques_liao)\
**Post date:** [November 9, 2022, 4:27pm UTC](https://community.forestadmin.com/t/can-we-make-a-relation-between-two-tables-that-are-in-different-databases/5497/10 "2022-11-09T16:27:05Z")

</div>

Yes, that was the solution !!!🙏
