Skip to content

Sequelize not creating association fields  #11280

Closed

Description

@simkessy

I'm a little confused with when Sequelize creates the fields for associations.

I've created my migrations using sequelize-cli. This generated a migration and model file. Then in the model file I populated my associations. Then ran npx sequelize-cli db:migrate.

This creates the tables but not the foreign keys needed for the associations defined in the model.

For example:
migration-quesions:

"use strict";
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable("questions", {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      category: {
        type: Sequelize.INTEGER
      },
      question: {
        type: Sequelize.STRING
      },
      createdAt: {
        allowNull: false,
        defaultValue: new Date(),
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        defaultValue: new Date(),
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable("questions");
  }
};

model-questions:

"use strict";
module.exports = (sequelize, DataTypes) => {
  const questions = sequelize.define(
    "questions",
    {
      question: DataTypes.STRING,
      weight: DateTypes.INTEGER
    },
    {}
  );
  questions.associate = function(models) {
    // associations can be defined here
    models.questions.hasOne(models.question_categories);
    models.questions.hasMany(models.question_answers);
  };
  return questions;
};

enter image description here

Activity

  1. papb commented on Aug 5, 2019

    @papb
    Member

    I believe the docs could use some extra clarification, indeed. The behavior you are seeing is correct, in principle. The migration is something that changes the database according to what you told it to do, there isn't much extra magic behind the scenes, so you have to put the foreign keys explicitly in your migration.

  2. added
    status: understood
    docs
    on Aug 5, 2019
  3. simkessy commented on Aug 5, 2019

    @simkessy
    Author

    I realized I could use the sync() method to have the database reflect the models. I don't see the point of the migration files however in that case. It's a lot simpler for me to setup the models then it is to go through and update migration files to add additional columns. Docs should be updated, it's not very clear.

  4. papb commented on Aug 17, 2019

    @papb
    Member

    Let me keep this open to clarify the docs 👍

  5. reopened this on Aug 17, 2019
  6. self-assigned this
    on Aug 17, 2019
  7. removed
    status: understood
    on Jan 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

docs

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions