mysql - db.sync({alter :true}). then(); 在 sequelize node.js express app

标签 mysql node.js express sequelize.js

我正在使用 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/

相关文章:

javascript - 从 anchor 标记获取元素 id 并替换 href

php - MySQL:如何将旧数据移至数据库

Node.js 和 node-sqlanywhere - 无可用连接

node.js - 多线程 - Node js 效率与 Apache

node.js - 如何记录通过 express.static 请求的文件

php - 从多个类别中获取数据

mysql - 通过 navicat ssh 隧道连接 mysql 时 ssh_options_set 中的参数无效

javascript - 防止删除相关模型实例 - Loopback 3

node.js - "this"构建后丢失? (ts/Node/express)

javascript - Express.js - 图像仅在一页上呈现