A
A
Alex102142021-07-30 14:32:34
Node.js
Alex10214, 2021-07-30 14:32:34

How to match models in Sequelize?

Hello, I have two models:

const Sequelize = require('sequelize');
const sequelize = require('../database/database')

const accounts_statistics = sequelize.define('accounts_statistics', {
  name: {
    type: Sequelize.STRING,
    allowNull: false,
  },
  value: {
    type: Sequelize.DECIMAL,
    allowNull: false,
  },
  system: {
    type: Sequelize.STRING,
    allowNull: false,
  },
  account: {
    type: Sequelize.STRING,
    allowNull: false,
  },
},
  {
  timestamps: false,
  freezeTableName: true,
});

accounts_statistics.removeAttribute('id');

module.exports = accounts_statistics;

const Sequelize = require('sequelize');
const sequelize = require('../database/database')

const statistics_description = sequelize.define('statistics_description', {
    name: {
      type: Sequelize.STRING,
      allowNull: false,
    },
    title: {
      type: Sequelize.STRING,
      allowNull: false,
    },
    short_table: {
      type: Sequelize.TINYINT,
      allowNull: false,
    },
    disp_order: {
      type: Sequelize.INTEGER,
      allowNull: false,
    },
  },
  {
    timestamps: false,
    freezeTableName: true,
  }
);

statistics_description.removeAttribute('id');

module.exports = statistics_description;


And there are two controllers :
const getAccounts_statistics = require('../models/Accounts_statistics');

module.exports.getAccounts_statistics = async (req, res) => {
  try {
    const arr = await getAccounts_statistics.findAll()
    res.status(201).json(arr);
  } catch (e) {
    console.log(e)
    res.status(500).json({
      message: 'Server ERROR!'
    })
  }
}

const getStatistics_description = require('../models/Statistics_description');

module.exports.getStatistics_description = async (req, res) => {
  try {
    const arr = await getStatistics_description.findAll()
    console.log(arr)
    res.status(201).json(arr);
  } catch (e) {
    console.log(e)
    res.status(500).json({
      message: 'Server ERROR!'
    })
  }
}

Each of these models has a "name" column and these columns are the same in both models (and the data in the columns is the same). I need to sort of glue these two models and get one. Tell me how to do it?

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