Sequelize making incorrect select

Asked

Viewed 687 times

1

I have an api in nodejs

www:

...
db.sequelize.transaction() 
    .then(() => { 
        server.listen(port); 
        server.on('error', onError); 
        server.on('listening', onListening);
})
...

models:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const RCC = sequelize.define('RCC', {
    RCC_RESP: DataTypes.STRING,
    RCC_CCT: DataTypes.STRING,   
    R_E_C_N_O_:{
      type: DataTypes.INTEGER,
      allowNull: false, //se aceita nulo
      autoIncrement: true,
      primaryKey: true
    },
    D_E_L_E_T_: {
      type: DataTypes.STRING(1),
      allowNull: false, //se aceita nulo
      autoIncrement: false
    }
  }, {});
  RCC.associate = function(models) {
    // associations can be defined here
  };
  return RCC;
};

controller:

class RCC {
    async show(db) {
        return await db.RCC.findAll({
            where: {
                RCC_CCT: '050101'
            }
        })
    }
}

export default new RCC();

Routes:

...
   routes.get('/showAll', async (req, res) => {
        const show = await RESP.show(db);
        if (show) {
            res.status(200).send(show)
        } else {
            res.status(404).json({ message: 'No Data Found' });
        }
    })

table name (not created by api) in my bd is: RCC, only when I give a console to see how the request is going, it gives a select in the table (there is no) Rccs, this is being standard.

Is there any way to change that? instead of doing a select in the Rccs table, actually do in the RCC table?

2 answers

1

Glad you solved, however, I noticed something different in the way of using the Sequelize Model.

Generally, you just need to import the model and use it to accomplish queries, example:

const RCCModel = require(path/to/models);
class RCC {
    async show(db) {
        return await RCCModel.findAll({
            where: {
                RCC_CCT: '050101'
            }
        })
    }
}

export default new RCC();

This is because in the models folder, there is a file with the following code:

'use strict'

const fs = require('fs')
const path = require('path')
const Sequelize = require('sequelize')
const basename = path.basename(__filename)
const config = require('../../config/database')
const db = {}

const sequelize = new Sequelize(
  config.database,
  config.username,
  config.password,
  config
)

fs.readdirSync(__dirname)
  .filter(file => {
    return (
      file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js'
    )
  })
  .forEach(file => {
    const model = sequelize['import'](path.join(__dirname, file))
    db[model.name] = model
  })

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db)
  }
})

db.sequelize = sequelize
db.Sequelize = Sequelize

module.exports = db

Especially in this file, db (same as yours) is injected into the models that are in the Models folder.

This setting is automatically created with the command $ sequelize init, through the sequeli-cli

  • but that’s exactly what I do

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.