Skip to content

Sequelize 7 should allow models to be registered on another instance when the original instance has been closed #16483

Closed

Description

@kenrick95

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

  1. added
    pending-approval
    type: bug
    on Sep 12, 2023
  2. ephys commented on Dec 13, 2023

    @ephys
    Member

    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 beforeConnect hook. 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.

  3. ephys commented on Jan 30, 2024

    @ephys
    Member

    Considering the above message, I will be closing this issue

  4. ephys commented on Feb 11, 2024

    @ephys
    Member

    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 ModelDefinition class 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.

  5. reopened this on Feb 11, 2024
  6. kenrick95 commented on Feb 21, 2024

    @kenrick95
    Author

    Thanks a lot for the support! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending-approvaltype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions