sequelize-typescript: Cannot read property 'getQueryInterface' of undefined

ts-node version 8.3.0 typescript version 3.8.3 node nersion 14.4.0 sequelize version 6.2.4 sequelize-typescript version 1.1.0

TypeError: Cannot read property 'getQueryInterface' of undefined
    at Function.get queryInterface [as queryInterface] (/Users/caiminhao/Documents/work/MOCAPE/backend/node_modules/sequelize/lib/model.js:55:27)
    at isFunctionMember (/Users/caiminhao/Documents/work/MOCAPE/backend/node_modules/sequelize-typescript/dist/model/model/model.js:99:25)
    at /Users/caiminhao/Documents/work/MOCAPE/backend/node_modules/sequelize-typescript/dist/model/model/model.js:96:5
    at Array.filter (<anonymous>)
    at Object.<anonymous> (/Users/caiminhao/Documents/work/MOCAPE/backend/node_modules/sequelize-typescript/dist/model/model/model.js:95:6)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Module._compile (/Users/caiminhao/Documents/work/MOCAPE/backend/node_modules/source-map-support/source-map-support.js:521:25)
    at Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Object.nodeDevHook [as .js] (/Users/caiminhao/Documents/work/MOCAPE/backend/node_modules/ts-node-dev/lib/hook.js:61:7)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
[ERROR] 17:57:04 TypeError: Cannot read property 'getQueryInterface' of undefined

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 54
  • Comments: 27

Commits related to this issue

Most upvoted comments

Guys please do not reply “same”, it does not add anything to the discution. Use the thumb up reaction on the top message if you want to show your interest in the resolution of this issue.

I have this error when using reference in one model and this model starts later (using ModelName.init) After change order of .init, solved my problem.

I solved it by downgrading sequelize to 5.21.10

same

I m getting this error too, is there any to solve this bug without downgrading the version ?

Also solved by downgrading sequelize (this time to 5.22.3)

EDIT: Already fixed in master.

While examining the stack trace, the issue is caused because when the sequelize-typescript#Model import is loaded, it scans all property names on the Sequelize#Model class to check if they are functions.

However, some are getters which require this.sequelize to be set which requires the Model to be instantiated with this.constructor.sequelize set to the sequelize instance.

@RobinBuschmann I think an easy fix is to add queryInterface and queryGenerator to the FORBIDDEN_KEYS list, as QueryInterface is already in there.

const staticModelFunctionProperties = object_1.getAllPropertyNames(sequelize_1.Model)
    .filter(key => !isForbiddenMember(key) &&
    isFunctionMember(key, sequelize_1.Model) &&
    !isPrivateMember(key));
function isFunctionMember(propertyKey, target) {
    return typeof target[propertyKey] === 'function';
function isForbiddenMember(propertyKey) {
    const FORBIDDEN_KEYS = ['name', 'constructor', 'length', 'prototype', 'caller', 'arguments', 'apply',
        'QueryInterface', 'QueryGenerator', 'init', 'replaceHookAliases', 'refreshAttributes', 'inspect'];
    return FORBIDDEN_KEYS.indexOf(propertyKey) !== -1;
}
class Model {
  static get queryInterface() {
    return this.sequelize.getQueryInterface();
  }

  static get queryGenerator() {
    return this.queryInterface.queryGenerator;
  }

  /**
   * A reference to the sequelize instance
   *
   * @see
   * {@link Sequelize}
   *
   * @property sequelize
   *
   * @returns {Sequelize}
   */
  get sequelize() {
    return this.constructor.sequelize;
  }

I was able to resolve that by upgrading also packages sequelize-cli and sequelize-typescript. so currently I have:

"sequelize": "^6.5.0",
"sequelize-cli": "^6.2.0",
"sequelize-typescript": "^2.1.0",

You can use @vjpr/sequelize-typescript@^1.1.0-vjpr.0 if you want to work with sequelize@6.

When using pnpm you can simply do:

package.json

"sequelize-typescript": "npm:@vjpr/sequelize-typescript@^1.1.0-vjpr.0"

It’s already fixed in master as mentioned here: #805 (comment)

@vjpr it also works by installing next npm install sequelize-typescript@next