Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

association helper methods do not appear to be created. #11393

Open
ccemeraldeyes opened this issue Sep 5, 2019 · 5 comments
Open

association helper methods do not appear to be created. #11393

ccemeraldeyes opened this issue Sep 5, 2019 · 5 comments
Labels
status: understood For issues. Applied when the issue is understood / reproducible. type: feature For issues and PRs. For new features. Never breaking changes.

Comments

@ccemeraldeyes
Copy link

ccemeraldeyes commented Sep 5, 2019

PROBLEM

Note: I cannot tell if this is a docs issue, a bug, or user error.

I have read through other seemingly-related bug reports (#7738, #6187, #10955) and lots of documentation and cannot find an answer.

My question is basically identical to #10955. I have defined associations on my models but when I try to call course.getCoursePaths(), I get an error that getCoursePaths()is not a function on course.

Note that I have also tried all variations I can think of re: using hasOne instead of belongsTo and using plural/singular

I am using sequelize orm version 5.18.1 and node 10.16.0

MIGRATIONS

These execute happily

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('courses', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      ...other fields....
      created_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
      },
    });
    await queryInterface.bulkInsert('courses', <some seed data>);

  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('courses');
  },
};
module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('course_paths', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      ...other fields
      created_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
      },
    });

    await queryInterface.bulkInsert('course_paths', <some seed data>);
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('course_paths');
  },
};
module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.addColumn('courses', 'path_id', {
      type: Sequelize.INTEGER,
      references: {
        model: 'course_paths',
        key: 'id',
      },
    });

    await queryInterface.<INSERT SOME SEED ASSOCIATIONS>

    // ADD NON-NULL CONSTRAINT AFTER POPULATION
    await queryInterface.changeColumn('courses', 'path_id', {
      type: Sequelize.INTEGER,
      references: {
        model: 'course_paths',
        key: 'id',
      },
      allowNull: false,
    });
  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.removeColumn('courses', 'path_id');
  },
};

MODELS

module.exports = (sequelize, DataTypes) => {
  const Course = sequelize.define(
    'course',
    {
      ....some other fields
      path_id: DataTypes.INTEGER,
    },
    {
      underscored: true,
    }
  );

  Course.associate = function(models) {
    // I HAVE ALSO TRIED THIS AS hasOne
    models.course.belongsTo(models.course_path, {
      // I HAVE ALSO TRIED THIS VALUE AS 'id' with sourceKey 'path_id', did not work
      foreignKey: 'path_id',
    });
  };
  return Course;
};
module.exports = (sequelize, DataTypes) => {
  const CoursePath = sequelize.define(
    'course_path',
    {
      ....you know, implicit integer id, other fields
      title: DataTypes.STRING,
    },
    {
      underscored: true,
    }
  );
  CoursePath.associate = function(models) {
    // I HAVE ALSO TRIED EXCLUDING THE FOLLOWING LINES
    models.course_path.hasMany(models.course, {
      foreignKey: 'path_id',
      sourceKey: 'id',
    });
  };
  return CoursePath;
};

CODE

const course = await db.course.findOne({
    where: {
      <some identifier>: <some course identifier>
    },
    // I HAVE ALSO TRIED OMITTING THIS
    include: [
      {model: db.course_path}
    ]
  });

// THIS IS WHERE IT ERRORS
// I HAVE ALSO TRIED getCoursePaths
// this succeeds with course.course_path
const path = await course.getCoursePath();

What is going on here? Why doesn't getCoursePath() exist?

@ccemeraldeyes
Copy link
Author

d'oh.

it's getCourse_path.

@papb
Copy link
Member

papb commented Sep 6, 2019

@ccemeraldeyes I struggled with this in the past as well 🤦‍♂️ thanks for reminding me of this. You probably agree with this behavior is ugly, right? I'll reopen this as a feature request to allow proper casing of this kind of thing.

@papb papb reopened this Sep 6, 2019
@papb papb added status: understood For issues. Applied when the issue is understood / reproducible. type: feature For issues and PRs. For new features. Never breaking changes. labels Sep 6, 2019
@papb papb self-assigned this Sep 6, 2019
@ccemeraldeyes
Copy link
Author

@papb ty! I saw that in the docs in some examples camelCase is the expectation for field names so I guess that's on us. Although none of the examples use multi-word field names! But yes, it would be great to convert entirely to camelCase for these methods :)

@iambastian
Copy link

I'm a bit late to the party, but I struggled with the same issue the last few days trying to find out whats wrong with my code.
It would be great if at least the docs would be edited to some multi-word names.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 7, 2021

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

@github-actions github-actions bot added the stale label Nov 7, 2021
@WikiRik WikiRik removed the stale label Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: understood For issues. Applied when the issue is understood / reproducible. type: feature For issues and PRs. For new features. Never breaking changes.
Projects
None yet
Development

No branches or pull requests

4 participants