首页 文章

db.sync({改变:真})然后();在sequelize node.js表达app

提问于
浏览
1

我使用sequelize作为myql数据库的一个orm,现在我面临的问题是,在我运行我的node.js express app超过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,
};

1 回答

相关问题