sequelize: Duplicated foreign key with alter option when using PostgreSQL Schemas
Issue Description
When you use PostgreSQL Schemas and { alter: true } option, each time the database is synchronized the foreign key is duplicated.
What are you doing?
SSCCE Link: https://github.com/papb/sequelize-sscce/pull/144
What do you expect to happen?
No duplicated foreign keys after each sync with { alter: true } option
What is actually happening?
Result after several application launch (npm run ts) and sync:

Additional context
Btw, even if models are defined on the same schema (e.g. User on schema1 and Product on schema1), the result is the same. The foreign key is duplicated each time I sync database.
The only way to prevent this duplication is to not use the property schema anymore and only use default one (public schema).
Environment
- Sequelize version: sequelize@5.22.3
- Node.js version: v12.16.3
- Operating System: MacOS Catalina 10.15.6
- If TypeScript related: TypeScript version: typescript@3.7.5 (didn’t tried without typescript, might be the same result I guess)
Issue Template Checklist
How does this problem relate to dialects?
- I think this problem happens only for the following dialect(s): postgres
Would you be willing to resolve this issue by submitting a Pull Request?
- No, I don’t have the time and I wouldn’t even know how to start.
About this issue
- Original URL
- State: open
- Created 6 years ago
- Comments: 15 (6 by maintainers)
I have found that if you put the tableName in lowercase, the foreign keys will not being duplicated any more
dialect: ‘mysql’
there is some issue with the equate in the model.js i found in the following path in function sync where we are checking for existing foreign keys, there we are actually checking for foreignKeyReference.referencedTableName === references.model
here references.model is an object, there we need to check for references.model.table or references.model.tableName
file path in the node modules is node_modules/sequelize/dist/lib/model.js