# Connect to multiple databases

**URL:** <https://community.forestadmin.com/t/connect-to-multiple-databases/396>\
**Category:** Help me!\
**Tags:** setup\
**Created:** [June 3, 2020, 5:21am UTC](https://community.forestadmin.com/t/connect-to-multiple-databases/396 "2020-06-03T05:21:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![thanhcongptit](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@thanhcongptit](https://community.forestadmin.com/u/thanhcongptit)\
**Post date:** [June 3, 2020, 5:21am UTC](https://community.forestadmin.com/t/connect-to-multiple-databases/396/1 "2020-06-03T05:21:33Z")

</div>

Dear supporters,

We connected to 3 different databases in our system. Unfortunately, they have some tables which have the same name and structure.  
For example:  
I have 3 databases named like “Salary”, “Employer”, “Finance”

- In Employer Database  
we have an employer, an address table, etc… An employer will have an address

- In Salary Database  
we have a salary and an address table, etc… A salary will have an address

- In Financer Database  
we have a finance and an address table, etc… A finance will have an address

ForestAdmin only shows data of an address table.

I changed my Sequelize model’s name look like  
**const Addresse = sequelize.define(‘address\_employers’)**  
I changed the name in my relationships as well.

But no luck, ForestAdmin still data of an address of one of three databases only.

 ![Screen Shot 2020-05-18 at 10.42.47 AM](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/1X/c732b13d408f371e7449015fa5d00c9e336728e7.png)  
 ![Screen Shot 2020-05-18 at 10.37.57 AM](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/1X/d67cf60ba076ea25056229b336d654c5df247f58.png)

 ![Screen Shot 2020-05-18 at 10.42.47 AM](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/1X/c732b13d408f371e7449015fa5d00c9e336728e7.png)  
Could you please give me your suggestion for my issue?

Thanks in advance,  
Cong Le

---

<div class="post-metadata">

**Author:** ![anon62739609](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon62739609](https://community.forestadmin.com/u/anon62739609)\
**Post date:** [June 3, 2020, 8:17am UTC](https://community.forestadmin.com/t/connect-to-multiple-databases/396/2 "2020-06-03T08:17:31Z")

</div>

Welcome @thanhcongptit to the Forest Admin Community ! 👋

I think you are on the right path but the implementation is not complete 😄.

Can you show me your `models/index.js` file please ?  
I think the issue might be in this file.

---

<div class="post-metadata">

**Author:** ![thanhcongptit](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@thanhcongptit](https://community.forestadmin.com/u/thanhcongptit)\
**Post date:** [June 3, 2020, 11:20pm UTC](https://community.forestadmin.com/t/connect-to-multiple-databases/396/3 "2020-06-03T23:20:29Z")

</div>

Hi Vince,

Thanks for your quick response.

This is my index.js in model folder

```javascript
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');

let databases = [
  {
  name: 'salaries',
  connectionString: process.env.DATABASE_URL_SALARIES
},
 {
  name: 'employers',
  connectionString: process.env.DATABASE_URL 
}, 
{
  name: 'financeurs',
  connectionString: process.env.DATABASE_URL_FINANCEURS
}];

const sequelize = {};
const db = {};
const models = {};

databases.forEach((databaseInfo) => {
  models[databaseInfo.name] = {};

  const isDevelopment = process.env.NODE_ENV === 'development' || !process.env.NODE_ENV;
  const databaseOptions = {
    logging: isDevelopment ? console.log : false,
    pool: { maxConnections: 10, minConnections: 1 },
    dialectOptions: {}
  };

  if (process.env.DATABASE_SSL && JSON.parse(process.env.DATABASE_SSL.toLowerCase())) {
    databaseOptions.dialectOptions.ssl = true;
  }

  const connection = new Sequelize(databaseInfo.connectionString, databaseOptions);
  sequelize[databaseInfo.name] = connection;

  fs
    .readdirSync(path.join(__dirname, databaseInfo.name))
    .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js')
    .forEach((file) => {
      try {
        const model = connection.import(path.join(__dirname, databaseInfo.name, file));
        models[databaseInfo.name][model.name] = model;
      } catch (error) {
        console.error('Model creation error: ' + error);
      }
    });

  Object.keys(models[databaseInfo.name]).forEach((modelName) => {
    if ('associate' in models[databaseInfo.name][modelName]) {
      models[databaseInfo.name][modelName].associate(sequelize[databaseInfo.name].models);
    }
  });
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

```

I uploaded my source code here **[https://drive.google.com/open?id=1VSPS4F2U\_xVc4BxmRBeX5taZAWolkbh9](https://drive.google.com/open?id=1VSPS4F2U_xVc4BxmRBeX5taZAWolkbh9)**  
Could you please take a look and help me know is there any wrong in my code.

Thank you so much!

---

<div class="post-metadata">

**Author:** ![rap2h](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/rap2h/32/25_2.png) [@rap2h](https://community.forestadmin.com/u/rap2h)\
**Post date:** [June 4, 2020, 12:53pm UTC](https://community.forestadmin.com/t/connect-to-multiple-databases/396/4 "2020-06-04T12:53:29Z")

</div>

Hi @thanhcongptit

Thanks to your code I guess I figured out the issue. 🙏

👉 Let me try to explain with a full yet simplified example (still highly inspired by yours!) with 2 databases. You could also have a look to the **TL;DR** at the end of this post.

1. Database **a** has 2 tables: **user** and **address**.
2. Database **b** has 2 tables: **company** and **address**.

The schema should look like this:

```sql
# Database a
create table address ( id serial constraint address_pk primary key );
create table "user" (
  id serial constraint user_pk primary key,
  address_id integer constraint user_address_id_fk references address 
);
# Database b
create table address ( id serial constraint address_pk primary key );
create table company (
  id serial constraint company_pk primary key,
  address_id integer constraint company_address_id_fk references address 
);

```

The model folder should look like this (yours is already correct):

![Capture d’écran 2020-06-04 à 14.16.56](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/1X/618366dd0446e4655bb90d6e70638547208beeb3.png)

Then each model file has to be edited 👇

### models/index.js

Yours is already correct, I just put it there since I hope it may help other readers.

```javascript
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');

let databases = [
  {
    name: 'a',
    connectionString: process.env.DATABASE_URL_A
  },
  {
    name: 'b',
    connectionString: process.env.DATABASE_URL_B
  }];

const sequelize = {};
const db = {};
const models = {};

databases.forEach((databaseInfo) => {
  models[databaseInfo.name] = {};

  const isDevelopment = process.env.NODE_ENV === 'development' || !process.env.NODE_ENV;
  const databaseOptions = {
    logging: isDevelopment ? console.log : false,
    pool: { maxConnections: 10, minConnections: 1 },
    dialectOptions: {}
  };

  if (process.env.DATABASE_SSL && JSON.parse(process.env.DATABASE_SSL.toLowerCase())) {
    databaseOptions.dialectOptions.ssl = true;
  }

  const connection = new Sequelize(databaseInfo.connectionString, databaseOptions);
  sequelize[databaseInfo.name] = connection;

  fs
    .readdirSync(path.join(__dirname, databaseInfo.name))
    .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js')
    .forEach((file) => {
      try {
        const model = connection.import(path.join(__dirname, databaseInfo.name, file));
        models[databaseInfo.name][model.name] = model;
      } catch (error) {
        console.error('Model creation error: ' + error);
      }
    });

  Object.keys(models[databaseInfo.name]).forEach((modelName) => {
    if ('associate' in models[databaseInfo.name][modelName]) {
      models[databaseInfo.name][modelName].associate(sequelize[databaseInfo.name].models);
    }
  });
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

```

### models/a/address.js

You have to define a _unique name_ for the address table (let’s use `user_address`)

```javascript
module.exports = (sequelize) => {
  const Address = sequelize.define('user_address', { // Change here.
  }, {
    tableName: 'address',
    timestamps: false,
    schema: process.env.DATABASE_SCHEMA,
  });
  Address.associate = (models) => {
    Address.hasMany(models.user, {
      foreignKey: {
        name: 'addressIdKey',
        field: 'address_id',
      },
      as: 'users',
    });
  };
  return Address;
};

```

### models/a/user.js

Then you have to update your `user` model to specify the name of the model used for relation (`models.user_address` in this case):

```javascript
module.exports = (sequelize) => {
  const User = sequelize.define('user', {
  }, {
    tableName: 'user',
    timestamps: false,
    schema: process.env.DATABASE_SCHEMA,
  });
  User.associate = (models) => {
    User.belongsTo(models.user_address, { // Change here.
      foreignKey: {
        name: 'addressIdKey',
        field: 'address_id',
      },
      as: 'address',
    });
  };
  return User;
};

```

### models/b/address.js

Define a _unique name_ for the address table in database b (let’s use `user_company`)

```javascript
module.exports = (sequelize) => {
  const Address = sequelize.define('company_address', { // Change here.
  }, {
    tableName: 'address',
    timestamps: false,
    schema: process.env.DATABASE_SCHEMA,
  });
  Address.associate = (models) => {
    Address.hasMany(models.company, {
      foreignKey: {
        name: 'addressIdKey',
        field: 'address_id',
      },
      as: 'companies',
    });
  };
  return Address;
};

```

### models/b/company.js

Then (last step 😅) you have to update your `company` model to specify the name of the model used for relation (`models.company_address` in this case):

```javascript
module.exports = (sequelize) => {
  const Company = sequelize.define('company', {
  }, {
    tableName: 'company',
    timestamps: false,
    schema: process.env.DATABASE_SCHEMA,
  });
  Company.associate = (models) => {
    Company.belongsTo(models.company_address, { // Change here.
      foreignKey: {
        name: 'addressIdKey',
        field: 'address_id',
      },
      as: 'company_address',
    });
  };
  return Company;
};

```

Then it should work and display this (see below) in Forest Admin interface. I’ve just tried this example and it works! 🎉

 ![Capture d’écran 2020-06-04 à 14.46.32](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/1X/d1efc3f39abb68a7ea0ac30f5d62ebd7899cdb40.png)

## TL;DR

Name your address models with two different names in their own files (e.g `sequelize.define('company_address'`) and in the file where they are referenced (e.g `Company.belongsTo(models.company_address`). Almost everything was already correct on your side. 👍

Let me know if it fixes your problem.

---

<div class="post-metadata">

**Author:** ![thanhcongptit](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@thanhcongptit](https://community.forestadmin.com/u/thanhcongptit)\
**Post date:** [June 5, 2020, 6:15am UTC](https://community.forestadmin.com/t/connect-to-multiple-databases/396/5 "2020-06-05T06:15:48Z")

</div>

Thanks for your solution.  
My problem has been solved  
My code is working fine now.

Thank you so much!
