Mrinal Verma
Mrinal Verma

Reputation: 359

db.sync({alter:true}).then(); in sequelize node.js express app

I am using sequelize as an orm for myql database , now the problem I am facing is that after I have run my node.js express app for more than 4 or 5 times I am getting this error 'Too many keys specified; max 64 keys allowed', now I want to know what is making this error appearing and can someone tell me a solution to solve this problem, till now I am solving this issue by replacing 'alter: true' with 'force: true' because of which I have to create my database again, I want to know is there a better way to solve this issue and give some insight on how does{alter: true} works

const Sequelize =require('sequelize');

const DataTypes= Sequelize.DataTypes;
const  config= require('../../config.json');
const db = new Sequelize(
    config.db.name,
    config.db.user,
    config.db.password
    ,{
        dialect:'mysql'
});

const categorie = db.define('categorie',{
   id: {
      type: DataTypes.INTEGER,
       primaryKey: true,
       autoIncrement: true
   },
   name:{
       type:DataTypes.STRING,
       unique:true,
   } ,
    tax:{
       type: DataTypes.FLOAT,
    }
});
const product =db.define('product',{
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    name:{
        type:DataTypes.STRING,
        alownull:true,
        unique:false
    },
    vendor:{
        type:DataTypes.STRING,
        unique:false,
        alownull:true,
    },
    price:{
        type:DataTypes.INTEGER,
    }
});
const user = db.define('user', {
    id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        allowNull: false,
        primaryKey: true,
    },
    name: {
        type: DataTypes.STRING,
        allowNull: false
    },
    password:{
        type: DataTypes.STRING,
        allowNull: false
    }
});

const cartItem = db.define('cartItem', {
    quantity: DataTypes.SMALLINT,
    amount: DataTypes.FLOAT,
    date:{
     type :DataTypes.DATE,
        allowNull: false
    },
    state:{
      type:DataTypes.STRING,
        allowNull: true,
    }
});

cartItem.belongsTo(product);   // cartitem will have a productid to access information form the product tables
user.hasMany(cartItem);        // many cartitem will have the userid to acess user information
product.belongsTo(categorie);  // product will have a categoryid to access information form the product tables

db.sync({alter:true}).then(() => "Database created"); // alter:true enables changes in the table
exports=module.exports={
    db,
    categorie,
    product,
    user,
    cartItem,
};

Upvotes: 2

Views: 6723

Answers (2)

rohit
rohit

Reputation: 11

for sequelize v6 -->> Use unique: 'compositeIndex' if u have two column with same name,

email: {
  type: DataTypes.STRING,
  allowNull: false,
  unique: 'compositeIndex'
},

Check at offical docs https://sequelize.org/docs/v6/core-concepts/model-basics/#column-options

Upvotes: 1

iwaduarte
iwaduarte

Reputation: 1698

This is a bug on Sequelize and they did not fix even after several reports.

https://github.com/sequelize/sequelize/issues/7915#issuecomment-314222662 https://github.com/sequelize/sequelize/issues/6134

The workaround solution is to use string instead of a boolean,

So the old:

column: {unique:true,    }

becomes:

column:{unique:'column'}

Cheers!

Upvotes: 5

Related Questions