Skip to content

error on sequelize raw query: query is not a function

I’m trying to use raw queries from sequelize in an express app. My folder structure is:


I want to use sequelize which I already define in /models/index.js from a controller.

This is /models/index.js:

"use strict";

var fs        = require("fs");
var path      = require("path");
var Sequelize = require('sequelize')
  , sequelize = new Sequelize(process.env.MYSQL_DB, process.env.MYSQL_USER, process.env.MYSQL_PASSWORD, {
      dialect: "mysql", // or 'sqlite', 'postgres', 'mariadb'
      port:    3306, // or 5432 (for postgres)

  .then(function(err) {
    console.log('Connection has been established successfully.');
  }, function (err) { 
    console.log('Unable to connect to the database:', err);

var db = {};
  .filter(function(file) {
    return (file.indexOf(".") !== 0) && (file !== "index.js");
  .forEach(function(file) {
    var model = sequelize.import(path.join(__dirname, file));
    db[] = model;

Object.keys(db).forEach(function(modelName) {
  if ("associate" in db[modelName]) {

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

module.exports = db;
module.exports.db = db;

I want to use a raw query in my price controller:

exports.index = function(req, res, next) {

    // var environment_hash = req.session.passport.user.environment_hash;

    var Price  = require('../models/index').Price;
    var db  = require('../models/index').db;


    db.query(`SELECT ... `).spread((results, metadata) => {
        // Results will be an empty array and metadata will contain the number of affected rows.

    var values = { 
                    where: { symbol: 'xxx' },                    

        .then(function(price) {
            res.render('home/home.ejs', {
                    price: price

But I’m getting this error message:

db: [Circular] }
TypeError: db.query is not a function

How can I fix this?


The Sequelize library is being assigned to a variable on the db object.

The error is in the second file instead of calling


We should call


In the first case we have called a function that does not exist. In another case we could assign the query function to a property on the db object.

db.query = db.sequelize.query

or you can de-structure using ES6

db.query = { query } = db.sequelize

Now we can run db.query as expected.