db.sync({alter:true}).then(); 在sequelize Node.js Express应用程序中

Mri*_*rma 2 mysql node.js express sequelize.js

我使用sequelize作为mysql数据库的orm,现在我面临的问题是,在运行我的node.jsexpress应用程序超过4或5次后,我收到此错误“指定的键太多;允许最多 64 个键”,现在我想知道是什么原因导致出现此错误,有人可以告诉我解决此问题的解决方案吗?到目前为止,我通过将 'alter: true' 替换为 'force: true' 来解决此问题,因为其中我必须再次创建数据库,我想知道是否有更好的方法来解决这个问题并提供一些有关 {alter: true} 如何工作的见解

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,
};
Run Code Online (Sandbox Code Playgroud)

iwa*_*rte 5

这是 Sequelize 上的一个错误,即使经过多次报告,他们也没有修复。

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

解决方法是使用字符串而不是布尔值,

所以旧的:

column: {unique:true,    }
Run Code Online (Sandbox Code Playgroud)

变成:

column:{unique:'column'}
Run Code Online (Sandbox Code Playgroud)

干杯!