Integration of a second database

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

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

Hi @jacques_liao

Can you tell us:

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

Hi @romaing
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).

Hello, do you have any news?

Hello @romaing , do you need more data ?

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.

1 Like

Hello, it works, thank you!

:sweat_smile:

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

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

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

Hello @romaing , do you have news :pray: ?

I’m working on it now

1 Like

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

models/database_2/some_model.js
image

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?

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

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

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.

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

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

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?

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

When I do a forest schema:update I have everything created
image

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

Should I create another folder outside the models folder?

Can you try with the following structure?

models/
   main/
     beneficiaries.js
     devices.js
     ...
   np/
     ...
1 Like