Getting a buffer memory error with Sequelize
I’m working on a Node.js project and keep hitting this ERR_BUFFER_OUT_OF_BOUNDS error when trying to sync my database. I have two models set up and I’m just calling sequelize.sync() to create the tables. I’m pretty new to this stack so I’m not sure what’s causing the problem.
The error happens whether the database exists or not. I even tried dropping the database manually through MySQL CLI and running it again, but same result.
The error message I get:
RangeError [ERR_BUFFER_OUT_OF_BOUNDS]: Attempt to access memory outside buffer bounds
at boundsError (internal/buffer.js:75:11)
at Buffer.readUInt8 (internal/buffer.js:243:5)
at Packet.type (/path/to/project/node_modules/tedious/lib/packet.js:143:24)
My database setup:
const { Sequelize } = require('sequelize')
const Author = require('./models/author')
const Article = require('./models/article')
const config = require('./database-config')
let database
if (process.env.NODE_ENV === 'production') {
database = new Sequelize(config.production)
} else if (process.env.NODE_ENV === 'development') {
database = new Sequelize(config.development)
} else {
database = new Sequelize(config.test)
}
const AuthorModel = database.define('author', Author)
const ArticleModel = database.define('article', Article)
AuthorModel.hasMany(ArticleModel)
ArticleModel.belongsTo(AuthorModel)
module.exports = {
database,
Author: AuthorModel,
Article: ArticleModel,
}
Author model definition:
const { DataTypes } = require('sequelize')
module.exports = {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true,
},
},
fullName: DataTypes.STRING,
hashedPassword: {
type: DataTypes.STRING,
allowNull: false,
},
isActive: {
type: DataTypes.BOOLEAN,
defaultValue: true,
},
}
Article model:
const { DataTypes } = require('sequelize')
module.exports = {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
headline: {
type: DataTypes.STRING,
allowNull: false,
validate: {
len: [3, 50],
},
},
body: {
type: DataTypes.TEXT,
allowNull: false,
validate: {
len: [10, 1000],
},
},
publishedAt: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
},
}
Database config:
const development = {
host: 'localhost',
database: 'my_app_dev',
username: 'root',
password: '',
dialect: 'mysql',
port: 3306,
}
const test = {
host: 'localhost',
database: 'my_app_test',
username: 'root',
password: '',
dialect: 'mysql',
port: 3306,
}
module.exports = { development, test }
Any ideas what might be wrong? The error seems to be coming from the tedious package but I’m using MySQL, not SQL Server.