Home > Mobile >  Sequelize model doesn't create all fields (columns) of every table
Sequelize model doesn't create all fields (columns) of every table

Time:10-25

I've created 15 models and only been getting every columns in few of the table randomly. What might be causing this? Can anyone help me?

PS: I've only included the parts of code that I thought are relevant to my question

app.js:

    // imports
const express = require('express');
const bodyParser = require('body-parser');

const app = express()
const port = 5000

//Body Parser
app.use(bodyParser.urlencoded({ extended: true }));

//Database connection

const db = require("./models");
db.sequelize.sync({ force: true }).then(() => {
    console.log("Drop and re-sync db.");
  });


//listen on port 5000
app.listen(port, () => console.info('Ready To Proceed'))

country.js(getting only default columns like id, createdAt and updatedAt)

module.exports = (sequelize, Sequelize) => {
    const Country = sequelize.define("country", {
      name: {
        type: Sequelize.STRING,
        unique: true
      },
      flag: {
          type: Sequelize.STRING
      },
      countryCode: {
          type: Sequelize.STRING
      }
    });
    return Country;
  };

cart.js(getting all columns I've defined)

module.exports = (sequelize, Sequelize) => {
    var User = sequelize.define('user');
    var Product = sequelize.define('product');
    const Cart = sequelize.define("cart", {
      quantity: {
        type: Sequelize.STRING
      }
    });
    Cart.belongsTo(User);
    Cart.belongsTo(Product);
    return Cart;
  };

index.js(to sync all models)

const dbConfig = require("../config/db.config");

const Sequelize = require("sequelize");
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
  host: dbConfig.HOST,
  dialect: dbConfig.dialect,
  port: dbConfig.PORT,
  operatorsAliases: false,

  pool: {
    max: dbConfig.pool.max,
    min: dbConfig.pool.min,
    acquire: dbConfig.pool.acquire,
    idle: dbConfig.pool.idle
  }
});

const db = {};

db.Sequelize = Sequelize;
db.sequelize = sequelize;

db.country = require("./country.model")(sequelize, Sequelize);
db.cart = require("./cart.model")(sequelize, Sequelize);

module.exports = db;

db.config.js:

module.exports = {
    HOST: "localhost",
    USER: "<<USER>>",
    PASSWORD: "<<Password>>",
    DB: "redhuntdb",
    dialect: "mysql",
    PORT: 3307(My db port is 3307),
    pool: {
      max: 15,
      min: 0,
      acquire: 30000,
      idle: 10000
    }
  };

CodePudding user response:

If anyone comes across this question and has the same problem, I figured out what I was doing wrong. sequelize.define() is creating a new table every time I use it. I was unknowingly using sequelize.define() to create an object of a model inside another model which just isn't right.

  • Related