How to define relationship when models are defined in separate files?
See original GitHub issueHi
I’m wondering what is the recommended way to define the relationship when the models are defined in separate files.
Assuming that I have a company model,
// models/Company.js
module.exports = function(sequelize, DataTypes) {
return sequelize.define('Company', {
}
};
and a project model,
// models/Project.js
module.exports = function(sequelize, DataTypes) {
return sequelize.define('Project', {
}
};
Then, I have an index.js that will read and import the above models,
// models/index.js
files.forEach(function(file) {
sequelize.import(file);
});
// sequelize.models now contains Company and Project
// then define the relationship: a Company has many Projects
sequelize.models.Company.hasMany(sequelize.models.Project);
Is this the right way to define the relationships? I can imagine that there will be a lot of relationships defined in the same file when there are a lot of models.
Issue Analytics
- State:
- Created 8 years ago
- Comments:40 (13 by maintainers)
Top Results From Across the Web
Define models' relations in models' files - sequelize.js
I define two models: User and Tag . User can have multiple tags, tags can belong to one user (1:m relation). I have...
Read more >Relationship models - IBM
Relationships are created by defining model data. There are primarily two different data models that are used within the relationship editor: relationship ......
Read more >Model Relationship | Holistics Docs
In Holistics 4.0, relationship definition is decoupled from data model. ... can be placed inside data model files, or dataset files or as...
Read more >Understanding Models, Classes and Relationships
The DSL Definition separates two aspects. The appearance of the model elements on the model diagram is defined by using shape classes and ......
Read more >Define a relationship
Relationships are defined in a properties file for the project, rather than in individual Word and Excel documents. This eliminates the need to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@mjangir Please do basic debugging on your own before taking up other peoples time, it’s the polite thing to do 😃
Basic debugging includes checking if and why a method is or is not called, this is basic javascript, nothing Sequelize specific.