# Integration of a second database

**URL:** <https://community.forestadmin.com/t/integration-of-a-second-database/4898>\
**Category:** Help me!\
**Tags:** howtos\
**Created:** [June 24, 2022, 2:37pm UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898 "2022-06-24T14:37:14Z")\
**Posts on this page:** 20\
**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:** [June 24, 2022, 2:37pm UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/1 "2022-06-24T14:37:14Z")

</div>

Hello,  
I am currently working on a new project and I am trying to integrate 1 additional database into the project.  
By following this tuto ([Add new databases - Developer guide](https://docs.forestadmin.com/documentation/how-tos/databases/add-new-databases)) I managed to get the 1 additional database. However, when I go to the forest administration site, I don’t know why my first database is not recognized anymore. Knowing that before integrating the new database, the 1st database works very well. On the other hand, the database that I integrated works well.

Here is the error that I currently have for one of my collections:

```javascript
[forest] 🌳🌳🌳 Unexpected error: relation "public.users" does not exist
{
  "name": "SequelizeDatabaseError",
  "parent": {
    "length": 111,
    "name": "error",
    "severity": "ERROR",
    "code": "42P01",
    "position": "33",
    "file": "parse_relation.c",
    "line": "1360",
    "routine": "parserOpenTable",
    "sql": "SELECT count(*) AS \"count\" FROM \"public\".\"users\" AS \"users\";"
  },
  "original": {
    "length": 111,
    "name": "error",
    "severity": "ERROR",
    "code": "42P01",
    "position": "33",
    "file": "parse_relation.c",
    "line": "1360",
    "routine": "parserOpenTable",
    "sql": "SELECT count(*) AS \"count\" FROM \"public\".\"users\" AS \"users\";"
  },
  "sql": "SELECT count(*) AS \"count\" FROM \"public\".\"users\" AS \"users\";",
  "stack": "SequelizeDatabaseError: relation \"public.users\" does not exist\n at Query.formatError (/Users/jacquesliao/stage/nostrumcare-forest-gateway/Nostrum Care Gateway/node_modules/sequelize/lib/dialects/postgres/query.js:354:16)\n at /Users/jacquesliao/stage/nostrumcare-forest-gateway/Nostrum Care Gateway/node_modules/sequelize/lib/dialects/postgres/query.js:71:18\n at tryCatcher (/Users/jacquesliao/stage/nostrumcare-forest-gateway/Nostrum Care Gateway/node_modules/sequelize/node_modules/bluebird/js/release/util.js:16:23)\n at Promise._settlePromiseFromHandler (/Users/jacquesliao/stage/nostrumcare-forest-gateway/Nostrum Care Gateway/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:547:31)\n at Promise._settlePromise (/Users/jacquesliao/stage/nostrumcare-forest-gateway/Nostrum Care Gateway/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:604:18)\n at Promise._settlePromise0 (/Users/jacquesliao/stage/nostrumcare-forest-gateway/Nostrum Care Gateway/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:649:10)\n at Promise._settlePromises (/Users/jacquesliao/stage/nostrumcare-forest-gateway/Nostrum Care Gateway/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:725:18)\n at _drainQueueStep (/Users/jacquesliao/stage/nostrumcare-forest-gateway/Nostrum Care Gateway/node_modules/sequelize/node_modules/bluebird/js/release/async.js:93:12)\n at _drainQueue (/Users/jacquesliao/stage/nostrumcare-forest-gateway/Nostrum Care Gateway/node_modules/sequelize/node_modules/bluebird/js/release/async.js:86:9)\n at Async._drainQueues (/Users/jacquesliao/stage/nostrumcare-forest-gateway/Nostrum Care Gateway/node_modules/sequelize/node_modules/bluebird/js/release/async.js:102:5)\n at Async.drainQueues [as _onImmediate] (/Users/jacquesliao/stage/nostrumcare-forest-gateway/Nostrum Care Gateway/node_modules/sequelize/node_modules/bluebird/js/release/async.js:15:14)\n at process.processImmediate (node:internal/timers:471:21)"
}

```

I thank you in advance for your help.

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [June 24, 2022, 2:48pm UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/2 "2022-06-24T14:48:10Z")

</div>

Hi @jacques_liao

Can you tell us:

- you project name / environnement
- which request are failing? (list/count/get? a smart action?)

---

<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:** [June 24, 2022, 3:07pm UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/3 "2022-06-24T15:07:16Z")

</div>

> [@anon39940173](#):
>
> Can you tell us:
> 
> - you project name / environnement
> - which request are failing? (list/count/get? a smart action?)

Hi @anon39940173  
Name project is Nostrum Care Gateway and I am currently on the development environment.  
All requests to type on my 1st database fail (list/count/get).

---

<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:** [June 27, 2022, 12:00pm UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/4 "2022-06-27T12:00:33Z")

</div>

Hello, do you have any news?

---

<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:** [June 29, 2022, 7:47am UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/6 "2022-06-29T07:47:08Z")

</div>

Hello @anon39940173 , do you need more data ?

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [June 29, 2022, 1:40pm UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/7 "2022-06-29T13:40:19Z")

</div>

Sorry, for the long delay… This thread went under the radar.

I’ll set up a test project and either tell you if I replicate your issue or send you a snippet to make it work.  
I should answer late this afternoon.

---

<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:** [June 30, 2022, 7:46am UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/8 "2022-06-30T07:46:29Z")

</div>

Hello, it works, thank you!

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [June 30, 2022, 7:55am UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/9 "2022-06-30T07:55:55Z")

</div>

😅

Can you tell me what changed so that we can create a ticket and fix the documentation?

---

<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:** [June 30, 2022, 9:16am UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/10 "2022-06-30T09:16:56Z")

</div>

So for adding a database it works fine and when I do a `forest schema:update` I have the data of both databases in my `.forestadmin-schema.json` file but when I go to the forest admin platform, only the data of the database I added works and displays me fine, otherwise for my first database I have the error that I sent in my first post

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [June 30, 2022, 9:41am UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/11 "2022-06-30T09:41:56Z")

</div>

> [@jacques\_liao](#):
>
> otherwise for my first database I have the error that I sent in my first post

> [@jacques\_liao](#):
>
> Hello, it works, thank you!

I’m unsure I understand your last reply.

Are you still having the issues you mentioned in the first thread?

---

<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:** [June 30, 2022, 9:45am UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/12 "2022-06-30T09:45:22Z")

</div>

> [@anon39940173](#):
>
> I’m unsure I understand your last reply.
> 
> Are you still having the issues you mentioned in the first thread?

yes I still have the same error

---

<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:** [July 4, 2022, 7:33am UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/13 "2022-07-04T07:33:00Z")

</div>

Hello @anon39940173 , do you have news 🙏 ?

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [July 4, 2022, 7:58am UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/14 "2022-07-04T07:58:18Z")

</div>

I’m working on it now

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [July 4, 2022, 3:32pm UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/15 "2022-07-04T15:32:53Z")

</div>

Hi @jacques_liao

Sorry again for letting this topic wait for so long, I’ve been super busy and could not find a way to keep up with support.

So I’ve setup a project with two postgres database, and finally managed to have the same error on my end by using different `schema` name for both databases.

Could this be what is happening on your end?

I’ve yet to find a satisfactory fix on my end, but can you try to:

1. Go to your `.env` file, and create a new environment variable (for instance `DATABASE_SCHEMA_SECOND_DB`)
2. Go to your `models/your_second_db` folder, and replace `schema: process.env.DATABASE_SCHEMA` by `schema: process.env.DATABASE_SCHEMA_SECOND_DB`

.env file  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/9/9716adb67db1807bdc4528a06654eec74fae9fce.png)

models/database\_2/some\_model.js  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/d/d18a2c04390f043faeea8dce59a0e7c2b494480d.png)

As a side note, the CLI does not seems to support using different schema names either (I did not see any parameter to provide the schema).

Did you have trouble when generating your model files? How did you solve them?

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [July 5, 2022, 6:53am UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/16 "2022-07-05T06:53:49Z")

</div>

If that does not work, can you make another check?

You should have two subfolders in your `models` folder (one for each database).

Can you add a `console.log(sequelize.config)` on the top of the file of a broken model, and check that it is pointing to the good database?

If not, it may mean that your models are not in the good folder (check your `forest/config.js` file)

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

---

<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:** [July 5, 2022, 5:37pm UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/17 "2022-07-05T17:37:51Z")

</div>

hello, it works, I’ll test everything tomorrow and I’ll let you know !

---

<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:** [July 6, 2022, 7:51am UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/18 "2022-07-06T07:51:04Z")

</div>

> [@anon39940173](#):
>
> Hi @jacques_liao
> 
> Sorry again for letting this topic wait for so long, I’ve been super busy and could not find a way to keep up with support.
> 
> So I’ve setup a project with two postgres database, and finally managed to have the same error on my end by using different `schema` name for both databases.
> 
> Could this be what is happening on your end?
> 
> I’ve yet to find a satisfactory fix on my end, but can you try to:
> 
> 1. Go to your `.env` file, and create a new environment variable (for instance `DATABASE_SCHEMA_SECOND_DB`)
> 2. Go to your `models/your_second_db` folder, and replace `schema: process.env.DATABASE_SCHEMA` by `schema: process.env.DATABASE_SCHEMA_SECOND_DB`
> 
> .env file  
> ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/9/9716adb67db1807bdc4528a06654eec74fae9fce.png)
> 
> models/database\_2/some\_model.js  
> ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/d/d18a2c04390f043faeea8dce59a0e7c2b494480d.png)
> 
> As a side note, the CLI does not seems to support using different schema names either (I did not see any parameter to provide the schema).
> 
> Did you have trouble when generating your model files? How did you solve them?

Hello, so for the .env I have 2 environment variables for each of my databases and for my 2nd connection to my database I have my 2nd variable.

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

I did encounter difficulties when generating my template files. I was able to solve them by putting the same `modeldir`.  
For example here I have the tables of my 1st database in white and those of the 2nd database in green  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/0/0616d46be1cfc0fee6632af85f6b930f0bc10f9e.png)

Concerning the test from the models I will tell you as soon as I finish the test.

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [July 6, 2022, 8:06am UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/19 "2022-07-06T08:06:01Z")

</div>

You definitively can’t put all models in the same folder: if you do Forest Admin cannot know which models are from which database, and will always query the first one which explains the behavior you are having.

Now that the models were generated, can you split your models in two folders and update the `modelsDir` key in the configuration file?

---

<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:** [July 6, 2022, 8:47am UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/20 "2022-07-06T08:47:48Z")

</div>

For my second database I created another folder in my `model` folder.

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

When I do a `forest schema:update` I have everything created  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/a/aed3b649138daeec769c39144c5550bdb0f0dad0.png)

However on forest admin I don’t see my first database at all

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

Should I create another folder outside the models folder?

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [July 6, 2022, 8:56am UTC](https://community.forestadmin.com/t/integration-of-a-second-database/4898/21 "2022-07-06T08:56:19Z")

</div>

Can you try with the following structure?

```javascript
models/
   main/
     beneficiaries.js
     devices.js
     ...
   np/
     ...

```

[Next page](https://community.forestadmin.com/t/integration-of-a-second-database/4898.md?page=2)
