• framework feathers ^ 2.2.3
  • ORM sequelize (feathers-sequelize ^ 2.3.2)
  • MySQL database
  • 2 tables between which you need to organize a connection

in the category.model.js file I describe the link:

 category.associate = function (models) { models.category.belongsTo(models.category_translate, { foreignKey: 'id', constraints: false, targetKey: 'category_id', as: 'translate' }); }; 

Now, if you contact the address localhost:3030/category then the result of this query is displayed:

 SELECT `id`, `icon`, `parent` FROM `category` AS `category` LIMIT 10; 

I tried localhost:3030/category?$select[]=translate to access the fields from the linked table, but an error occurred.

What am I doing wrong?

How to access data from another table by linking from the current model?

    1 answer 1

    The problem was that you needed to make a hook that would include a link to the requests. I created translateHook.js

     module.exports = function (options = {}) { return function categoryTranslate (hook) { const AssociatedModel = hook.app.services['category-translate'].Model; hook.params.sequelize = { include: [{ model: AssociatedModel, as: 'translate', query: { $limit: 1, $select: ['country_icon'] }, where: { lang: hook.params.query['$language'] } }] }; delete hook.params.query['$language']; return Promise.resolve(hook); }; }; 

    source here .