-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Circular Node Module Requiring Causes DataTypes to be Empty Object #9186
Comments
This seems odd, can you post the actual code you are writing? iirc you shouldn't import sequelize submodules (and that seems like what you are doing). |
@SimonSchick However, if you look at these two files you can clearly see circular module requiring going on: data-types.js So Do you see what I mean? |
Please provide me a SSCCE. It will help me understand your problem quickly. ( What is SSCCE ? You can find template for Sequelize here ) A few good example of SSCCE
Node is able to handle circular dependencies by deferring object construction, and if it was a real issue we should not be able to build this project on CI. Please include a code sample that really reproduce this issue because it seems some usual userland issue |
I will dive deeper into my own codebase to see if there is an issue on my end. I tried learning more about how node handles circular dependencies and the only real documentation I could find was this: Do you have any other references/links I could check out? Specifically around deferred object construction you mentioned? I really want to learn more for my own project. |
https://nodejs.org/api/modules.html#modules_cycles explains what I meant, if you require a module You are trying to include Generally you can avoid cycling dependencies by
|
I did a global search in my codebase for references of const SqlString = require('sequelize/lib/sql-string'); I added that a while ago when we were still on We have since upgraded to Once I removed that line I no longer experienced this issue. Thanks for the feedback @SimonSchick and @sushantdhiman, this issue can be closed. |
If you want to escape/quote something you should use this const sequelize = /** get your sequelize instance */
const qi = sequelize.getQueryInterface();
// qi.quoteIdentifier, for quoting strings
// qi.escape , for escaping strings |
What are you doing?
I'm trying to use sequelize with tables that contain columns with defaultValues, like:
Expected Result
The
data-types.js
module required insequelize/lib/utils.js
returns a non-empty DataTypes objectActual Result
The
data-types.js
module required insequelize/lib/utils.js
returns an empty DataTypes object which causes various errors to occur when running my app, including:Root Cause
Node circular module dependency between
sequelize/lib/utils.js
andsequelize/lib/data-types.js
See screenshots below for details
Screenshots
Step 1) Notice Circular Node Module Dependencies - utils.js >> data-types.js >> utils.js
Step 2) Add Logging When Modules Are Required
Step 3) Add Logging When DataTypes is Exported
Step 4) Start App and Notice Empty Data Types Object
Step 5) Comment Out
./utils.js
Module Requires to Eliminate Circular DependencyStep 6) Restart App and Notice Data Types Is Exported Before utils.js Log Statement Prints
Step 7) Notice After All DataTypes from data-types.js Log Statment Are Printed We See utils.js Log Statement with Legit DataTypes Object
Proposed Resolution
Refactor utils.js to move string formatting into it's own module. Then data-types would just require that module since that's all it was using utils.js for.Update
utils.js is only referenced once in all of
data-types.js
file, on line #27in module
utils.js
, warn is exported asIt seems as though logger is instantiated and exported as a singleton here, probably for convenience. One option could be to move logging into it's own module. Another (probably less painful) option is to use something else to log for the one line in
data-types.js
moduleNode Circular Dependency Reading
http://blog.cloudmineinc.com/managing-cyclic-dependencies-in-node.js?hsFormKey=ae1b2c3916d0a2be7aafb5d841ce24c0
http://selfcontained.us/2012/05/08/node-js-circular-dependencies/
Dialect: mysql
Database version: 5.6.34
Sequelize version: 4.36.1
Tested with latest release: Yes
The text was updated successfully, but these errors were encountered: