【问题标题】:db.sync({alter:true}).then(); in sequelize node.js express appdb.sync({alter:true}).then();在 sequelize node.js express 应用程序中
【发布时间】: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


    【解决方案1】:

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

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

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

    那么老:

    column: {unique:true,    }
    

    变成:

    column:{unique:'column'}
    

    干杯!

    【讨论】:

    • 这将解决问题,如果索引名称未作为字符串提供,sequelize 将尝试以不同名称创建索引(column_1, column_2, column_3 用于每次应用程序启动)。跨度>
    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2021-04-30
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 2011-06-09
    相关资源
    最近更新 更多