Sequelize magic association method not recognise by WebStorm
Please can anyone tell me how to make WebStorm recognise magic association methods in WebStorm?

The code is working fine without error but these methods are basically not being reconciled by the IDE which is very frustrating
Please sign in to leave a comment.
What are req and user here? Please share a complete code snippet/files the issue can be reproduced with
Elena Pogorelova Hi.. It is just how sequelize creating relation and associate them. Here is the link to docs here . Reproducible code would be like so and it will be long..
Create a database
const Sequelize = require('sequelize').Sequelize;
const sequelize = new Sequelize('test-schema', 'root', 'password', {dialect: 'mysql', host: 'localhost'});
module.exports = sequelize;
create a models
const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const User = sequelize.define('user', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true
},
name: Sequelize.STRING,
email: Sequelize.STRING
});
module.exports = User;
const Sequelize = require('sequelize');
const sequelize = require('../util/database')
const Product = sequelize.define('product', {
id: {type: Sequelize.INTEGER, autoIncrement: true, allowNull: false, primaryKey: true},
title: Sequelize.STRING,
price: {type: Sequelize.DOUBLE, allowNull: false}
});
module.exports = Product;
create relations and add user so it can be accessed globally
app.use((req, res, next) => {
User.findByPk(1)
.then(user => {
req.user = user;
next();
})
.catch(err => console.log(err))
})
Product.belongsTo(User, {constraints: true, onDelete: 'CASCADE'})
User.hasMany(Product);
write controller for creating product where the warning is displayed
exports.addProduct = (req, res, next) => {const title = req.body.title;
const price = req.body.price;
req.user.createProduct({ //<=== here is the warning
title: title,
price: price,
})
.then((result) => res.status(200)
.send(result))
.catch((err) => console.log(err));
};
createProduct methode is automatically generated by sequelize when relation is created. (create to create a record to table and Product by the name of the relation table) Theres also other methods like getProduct and so on which also are not recognised by IDE. Codes running without any problem. It just that it is not recognised by IDE
I forgot to mention that the method above createProduct as equivalent to below adding record.
Product.create({title: title,
price: price,
userId: req.user.id
})
I see. So, the method is not defined statically anywhere, it's generated dynamically in runtime, right? in such case, there is no way for the IDE to resolve it using static code analysis.... Recognizing such methods would require adding special support for Sequelize
Elena Pogorelova So, the method is not defined statically anywhere, it's generated dynamically in runtime, right? That is correct. Is there something I can do to add that special support or does it have to be done by the WebStorm? I tried to open it in VS code and I didn't get that warning there
VSCode doesn't show any warnings for unresolved methods. You can disable WebStorm inspections if shutting them up, making the code 'green', is the only requirement:) Like:
Adding support for this module requires developing a custom plugin. You can find basic documentation about plugin development here: http://www.jetbrains.org/intellij/sdk/docs/. Some open source plugins can be found at https://github.com/JetBrains/intellij-plugins
You can file a feature request for providing Sequelize support to youtrack, https://youtrack.jetbrains.com/issues/WEB
Elena Pogorelova Thank for the answer... would you be so kind and also answer it on SO... Thanks
Done:)