Repository navigation
Description
Issue Creation Checklist
- I understand that my issue will be automatically closed if I don't fill in the requested information
- I have read the contribution guidelines
Bug Description
#15389 blocks from registering a model on another instance even if the original instance has been closed.
The use case I have is that I have a service that has dynamic config listener so I could change the database connection credentials on runtime. To achieve this, I would close the db connection and then re-initialize db connection with the new config. However, at this point, I couldn't do so since I will be blocked by this error:
Error: Model Configuration has already been initialized. Models can only belong to one Sequelize instance. Registering the same model with multiple Sequelize instances is not yet supported. Please see https://github.com/sequelize/sequelize/issues/15389
at registerModelDefinition (/node_modules/.pnpm/@[email protected][email protected]/node_modules/@sequelize/core/lib/model-definition.js:625:11)
at initModel (/node_modules/.pnpm/@[email protected][email protected]/node_modules/@sequelize/core/lib/model-typescript.js:326:55)
at initDecoratedModel (/node_modules/.pnpm/@[email protected][email protected]/node_modules/@sequelize/core/lib/decorators/shared/model.js:108:41)
at /node_modules/.pnpm/@[email protected][email protected]/node_modules/@sequelize/core/lib/sequelize-typescript.js:119:91
at Array.filter (<anonymous>)
at DbClient.addModels (/node_modules/.pnpm/@[email protected][email protected]/node_modules/@sequelize/core/lib/sequelize-typescript.js:119:37)
Perhaps when sequelize.close() is called, it should remove the related models from model-definition.ts's modelDefinitions weakmap?
Reproducible Example
Here is the link to the SSCCE for this issue: https://github.com/kenrick95/sequelize-sscce-model-reuse
PR for the CI run here: sequelize/sequelize-sscce#263
What do you expect to happen?
Model can be registered on another Sequelize instance when the original Sequelize instance has been closed.
What is actually happening?
Model cannot be registered on another Sequelize instance when the original Sequelize instance has been closed.
Environment
- Sequelize version: 7.0.0-alpha.27
- Node.js version: 16.20.1
- If TypeScript related: TypeScript version: 5.1.3
- Database & Version: mysql 5.7.29
- Connector library & Version: [email protected]
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I will need guidance.
- No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
- No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.
Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.
Activity
ephys commentedon Dec 13, 2023
Unfortunately there are too many unknowns to easily disable this safeguard. Too many things rely on the Sequelize instance once models are initialized. We're slowly working on cleanly separating concepts but by the time we're done, #15389 will basically be ready
What I can recommend, if what you need is limited to changing the credentials, would be to update the configuration passed to the
beforeConnecthook. Here is another thread that talks about this: #14136 (comment)You can also kill all connections by calling
sequelize.connectionManager.pool.destroyAllNow(), instead of closing Sequelize itself.ephys commentedon Jan 30, 2024
Considering the above message, I will be closing this issue
ephys commentedon Feb 11, 2024
Following PR #17077, you should be able to register a model again after calling the upcoming
sequelize.removeAllModels()method.The addition is somewhat accidental: I wanted to make models only usable if they were associated with a sequelize instance, and it was already possible to remove a model from
sequelize.models, so I made a change that made this also unregister the model.I decided to not add a flag that prevents further usage of the model, so the model can be initialized again, against any Sequelize instance.
I have to warn however that even though a lot of work has gone into making the
ModelDefinitionclass the source of truth, I cannot currently guarantee you won't experience any weird behavior if you do this. Considering it fits well with the planned design, we'll fix any bug that may occur instead of blocking the ability to do this.kenrick95 commentedon Feb 21, 2024
Thanks a lot for the support! 🎉