我正在使用 sequelize 作为 myql 数据库的 orm,现在我面临的问题是,在我运行我的 node.js express 应用程序超过 4 或 5 次后,我收到此错误“指定的键太多;”最多允许 64 个键',现在我想知道是什么导致出现这个错误,有人可以告诉我解决这个问题的方法,直到现在我通过用 'force: true' 替换 'alter: 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,
};
最佳答案
这是 Sequelize 上的一个错误,即使在多次报告后他们也没有修复。
https://github.com/sequelize/sequelize/issues/7915#issuecomment-314222662 https://github.com/sequelize/sequelize/issues/6134
解决方法是使用字符串而不是 bool 值,
所以旧的:
column: {unique:true, }
变成:
column:{unique:'column'}
干杯!
关于mysql - db.sync({alter :true}). then(); 在 sequelize node.js express app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48637184/