Setup fails with SequelizeAssociationError

Hello, I just found this tool and wanted to test it.
After following the instructions on https://app.forestadmin.com (creating account, creating project, setup via npm) I receive the error below.

The same error appears when I run it via docker using the latest image.

Expected behavior

Setup should work

Actual behavior

Setup fails with error SequelizeAssociationError

Failure Logs

➜ xxx-test npm start

xxx-test@0.0.1 start /Users/pasukaru/xxx-test
node ./server.js

/Users/pasukaru/xxx-test/node_modules/sequelize/lib/associations/base.js:106
      throw new AssociationError(`You have used the alias ${options.as} in two separate associations. ` +
      ^

SequelizeAssociationError: You have used the alias partner in two separate associations. Aliased associations must have unique aliases.
    at new Association (/Users/pasukaru/xxx-test/node_modules/sequelize/lib/associations/base.js:106:13)
    at new BelongsTo (/Users/pasukaru/xxx-test/node_modules/sequelize/lib/associations/belongs-to.js:18:5)
    at Function.<anonymous> (/Users/pasukaru/xxx-test/node_modules/sequelize/lib/associations/mixin.js:105:25)
    at Function.Ad.associate (/Users/pasukaru/xxx-test/models/ad.js:51:8)
    at /Users/pasukaru/xxx-test/models/index.js:39:19
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/Users/pasukaru/xxx-test/models/index.js:37:17)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/Users/pasukaru/xxx-test/routes/ad.js:3:16)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at /Users/pasukaru/xxx-test/node_modules/require-all/index.js:56:46
    at Array.forEach (<anonymous>)
    at requireAll (/Users/pasukaru/xxx-test/node_modules/require-all/index.js:34:9)
    at Object.<anonymous> (/Users/pasukaru/xxx-test/app.js:47:1)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/Users/pasukaru/xxx-test/server.js:2:13)

Context

Fresh install.

“dependencies”: {
“chalk”: “~1.1.3”,
“cookie-parser”: “1.4.4”,
“cors”: “2.8.5”,
“debug”: “~4.0.1”,
“dotenv”: “~6.1.0”,
“express”: “~4.16.3”,
“express-jwt”: “5.3.1”,
“forest-express-sequelize”: “^6.0.0”,
“morgan”: “1.9.1”,
“require-all”: “^3.0.0”,
“sequelize”: “~5.15.1”,
“pg”: “~6.1.0”
}

Postgres version: 11.6
npm version: 6.14.4
node version: v10.20.1

Hi @Pasukaru, and welcome to our community !

I think you are having an issue with an alias generated with the same name as a field/another alias. You should be able to fix this by editing the model definition directly, since Sequelize does not handle non unique alias names (All the models are generated within the models folder)

If you need some help to fix the issue, I will need to see the generated models which are causing this issue.

Regards

Hello,

Thanks for the quick reply!
I just took a look at the models (Partner entity).
It looks like model is not generated correctly.

More specifically, i found this part:


  // This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-relationships.
  Partner.associate = (models) => {
    Partner.hasMany(models.ad, {
      foreignKey: {
        name: 'partnerIdKey',
        field: 'partnerId',
      },
      as: 'ads',
    });
    Partner.hasMany(models.ad, {
      foreignKey: {
        name: 'partnerIdKey',
        field: 'partnerId',
      },
      as: 'ads',
    });
    Partner.hasMany(models.ad, {
      foreignKey: {
        name: 'partnerIdKey',
        field: 'partnerId',
      },
      as: 'ads',
    });
    Partner.hasMany(models.ad, {
      foreignKey: {
        name: 'partnerIdKey',
        field: 'partnerId',
      },
      as: 'ads',
    });

(it coninues on for a while the same duplication).

I’ll see if I can fix this myself and keep you posted.

Update:

This appears to be the same in all entities. Everything is duplicated.
Also enums have their values duplicated.

I think the issue is that in my local db i have multiple postgres schemata with the same tables (for integration testing. I have public,test0,test1,…).

Maybe this is confusing the tool and causing the duplications.
I did specify the main schema during setup.
Will try to reset everything with only one schema available.

Seems strange to me since you have to specify which DB to choose during the onboarding process, and models should be generated for that DB…

Just did a quick test the latest lumber version and a similar setup without any issue :confused:

Would you mind sharing also the referenced ad model so I can give it a shot with a similar definition to reproduce ?

Another update:

Dropping all schemas, and only leaving the main schema solved the issue.
I just did the setup process again, and now the app is able to start.

Will try to provide a minimal example to reproduce.

brb

1 Like

Hm, I’m also unable to reproduce the duplication.

I setup the postgres db like this, and it works:

CREATE SCHEMA something;
CREATE SCHEMA something2;

CREATE TABLE something.test (id UUID PRIMARY KEY );
CREATE TABLE something.test2 (id UUID PRIMARY KEY, fk_test UUID REFERENCES something.test (id));

CREATE TABLE something2.test (id UUID PRIMARY KEY );
CREATE TABLE something2.test2 (id UUID PRIMARY KEY, fk_test UUID REFERENCES something2.test (id));

So, I’m not sure what specific part of my previous db setup caused the model generation issues.

Pretty strange. I just tried creating a new project with your script, and no code duplication either …
I’ll definitely try again today, just in case I can find any way to duplicate your original issue.

Now everything works on your side, right ?

With the other schemata removed it worked, yes.
Have not tried it again with all of them.

2 Likes