【发布时间】:2018-02-06 07:00:43
【问题描述】:
我正在使用 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,
};
【问题讨论】:
标签: mysql node.js express sequelize.js