question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to define relationship when models are defined in separate files?

See original GitHub issue

Hi

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:closed
  • Created 8 years ago
  • Comments:40 (13 by maintainers)

github_iconTop GitHub Comments

103reactions
mickhansencommented, May 31, 2016
module.exports = function(models) {
  // setup relations
};
68reactions
mickhansencommented, May 31, 2016

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found