0

I am facing an issue related to sequelize when generating the database by commandline

npx sequelize-cli db:migrate

sequelize-cli error

The firsting I installing my sql and using MySQL Workbench to connect it -> successfully Then I created a new database named car-rental

This is my code (all data was generated by sequelize-cli)

package.json

"dependencies": {
    "express": "^4.19.2",
    "express-handlebars": "^7.1.3",
    "mysql2": "^3.10.1",
    "sequelize": "^6.37.3"
  },
  "devDependencies": {
    "sequelize-cli": "^6.6.2"
  }

config\config.json

{
  "dev": {
    "username": "<my_name>",
    "password": "<my_password>",
    "database": "car-rental",
    "host": "127.0.0.1",
    "dialect": "mysql",
    "port": 12321
  }
}

migrations\create-companies.js

'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('Companies', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      companyId: {
        allowNull: false,
        type: Sequelize.UUIDV4
      },
      companyName: {
        type: Sequelize.STRING
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('Companies');
  }
};

modals\index.js

'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const config = require(__dirname + '/../config/config.json')['dev'];
const db = {};

let sequelize = new Sequelize(config.database, config.username, config.password, {
  host: config.host,
  dialect: config.dialect,
  port: config.port
});

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (
      file.indexOf('.') !== 0 &&
      file !== basename &&
      file.slice(-3) === '.js' &&
      file.indexOf('.test.js') === -1
    );
  })
  .forEach(file => {
    const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

modals\companies.js

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Companies extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  }
  Companies.init({
    companyId: {
      allowNull: false,
      type: DataTypes.UUIDV4
    },
    companyName: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'Companies',
  });
  return Companies;
};

Could you help me please take a look on this? Thank you.

1 Answer 1

0

Try connecting to the database this way First Create file .env which includes the following code

#database
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=
MYSQL_PORT=3306
MYSQL_DATABASE=databaseName

After this, write the connection code

const mysql = require('mysql2');
const connection = mysql.createConnection({
    host: process.env.MYSQL_HOST,
    user: process.env.MYSQL_USER,
    password: process.env.MYSQL_PASSWORD,
    database: process.env.MYSQL_DATABASE,
    port: process.env.MYSQL_PORT
});

module.exports = connection.promise();

sample use code

const db = require('../database/mysql')

exports.findAll = async ()=>{
    const [row , fields] = await db.query(`
        SELECT *
        FROM tb
    `);
    console.log(row);
    return row;
}
2
  • I am using code first with Sequelize framework
    – hhpr98
    Commented Jun 30 at 12:20
  • Try console.log the "config" object in "modals.js" and see what is it's content. Most probably the "delicate" property in "undefined" Commented Jun 30 at 17:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.