Skip to content

Add example for model relations #20

Description

@kulakowka

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.

Activity

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions