J
J
jenya77712018-07-18 15:39:35
Node.js
jenya7771, 2018-07-18 15:39:35

How to join in Sequelize using id array?

Hello, there are 2 tables:
With ads

const ads = sequelize.define('ads', {
  user_panel_id: { 
    type: Sequelize.INTEGER,
    allowNull: false
  },
  name: { 
    type: Sequelize.STRING,
    allowNull: false
  },
  categories: { 
    type: Sequelize.ARRAY(Sequelize.INTEGER),
    allowNull: false
  },
}, {
  charset: 'utf8',
  collate: 'utf8_unicode_ci',
  timestamps: true,
})

And with categories
const categories = sequelize.define('categories', {
  name: { 
    type: Sequelize.STRING,
    allowNull: false
  },
}, {
  charset: 'utf8',
  collate: 'utf8_unicode_ci',
  timestamps: true
})

The query I am using
models.ads
  .findOne({
    where: {
      id: 1
    },
    include: [{
      model: models.categories
    }],
  })
  .then(data => {
    console.log(data)
  })
  .catch(error => {
    console.error(error)
  })

How to connect these 2 tables so that the query would work and return all categories from the declaration array?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question