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.

Add example for model relations

See original GitHub issue

Can somebody show an example of creating relations between the models?

I want to relate a model Tutorial and Comment. How can i do that?

I make it so:

'use strict'

const Sequelize = require('sequelize')

module.exports = function (sequelize) {
  const comment = sequelize.define('comment', {
    content: {
      required: true,
      type: Sequelize.TEXT,
      allowNull: false
    },
    tutorialId: {
      required: true,
      type: Sequelize.INTEGER
    }
  }, {
    timestamps: true,
    freezeTableName: true
  })

  comment.belongsTo(sequelize.models.tutorial)

  comment.sync({
    force: true
  })

  return comment
}

But sometimes, when i start my application i have error:

Unhandled rejection SequelizeDatabaseError: relation "tutorial" does not exist

I used to work only with mongoDB. But I think I need to learn sequelize.

It would be great if we had more examples that show how to work with the database, how to set up the relationship between models and between services. I read the documentation, but it very much and I’m not like I can not configure it to work properly.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:5
  • Comments:24 (10 by maintainers)

github_iconTop GitHub Comments

44reactions
lopezjuripcommented, Feb 14, 2017

My current approach:

// src/services/organization/organization-model.js

const Sequelize = require('sequelize');

module.exports = function(sequelize) {
  const organization = sequelize.define('organization', {
    name: {
      type: Sequelize.STRING,
      allowNull: false,
    },
  }, {
    classMethods: {
      associate(models) {
        organization.hasMany(models.user);
      },
    },
  });

  return organization;
};
// src/services/index.js

const Sequelize = require('sequelize');

const authentication = require('./authentication');
const user = require('./user');
const organization = require('./organization');

module.exports = function() {
  const app = this;

  const sequelize = new Sequelize(app.get('postgres'), {
    dialect: 'postgres',
    logging: false, // console.log,
  });
  app.set('sequelize', sequelize);

  app.configure(authentication);
  app.configure(user);
  app.configure(organization);

  // Setup relationships
  const models = sequelize.models;
  Object.values(models)
    .filter(model => model.associate)
    .forEach(model => model.associate(models));

  sequelize.sync();
};
7reactions
kulakowkacommented, Feb 29, 2016

It seems I figured.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating model relations | LoopBack Documentation
The easiest way to create a new relation between existing models is to use the slc loopback:relation , or the apic loopback:relation ,...
Read more >
Adding cross-model relations - Bizzdesign Support
Cross-model relations can be added between objects, but also between objects and diagrams or views, and between diagrams and/or views. A single ...
Read more >
Relationships between tables in a Data Model
A relationship is a connection between two tables of data, based on one column in each. A workbook can store each piece of...
Read more >
Models with Relationships in FastAPI - SQLModel
We cannot simply include all the data, including all the internal relationships, because each hero has an attribute team with their team, and...
Read more >
Eloquent: Relationships - The PHP Framework For Web Artisans
It is possible to construct more advanced "has one of many" relationships. For example, a Product model may have many associated Price models...
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