association helper methods do not appear to be created.
See original GitHub issuePROBLEM
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?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Polymorphic/Helper Association methods do not appear in ...
Polymorphic/Helper Association methods appear not to be created in Typescript and not available with dot notation from IDE.
Read more >Helper methods and associations with include not working on ...
As you can see I have created the associations on category (hasMany) and on ticket (belongsTo). The foreignKey should only be added to...
Read more >Association
In this case we can create a class containing the helper methods. In this case the helper methods would be public. We call...
Read more >Associations - Sequelize
To do this, Sequelize provides four types of associations that should be combined to create them: The HasOne association; The BelongsTo ...
Read more >An Intro to Partials and Helpers in Ruby on Rails
If the built-in helpers don't fit your needs, you can also build your own custom helper methods. Where to store custom helpers. Custom...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
d’oh.
it’s getCourse_path.
@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 😃