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

Created on 1 Jul 2020  路  18Comments  路  Source: RobinBuschmann/sequelize-typescript

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

Most helpful comment

I solved it by downgrading sequelize to 5.21.10

All 18 comments

I solved it by downgrading sequelize to 5.21.10

Same problem here

Also solved by downgrading sequelize (this time to 5.22.3)

I was on Sequelize 5.21.13 and upgraded to 6.3.4 and got the same error. Am reverting back to 5.21.13 until this has been fixed.

Same

same problem here ):

same

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.

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;
  }

same

ts-node version ^9.0.0
typescript version ^4.0.5
node version 15.0.1
sequelize version 6.3.5
sequelize-typescript version ^1.1.0

The error still persists in version 6.3.5

TypeError: Cannot read property 'getQueryInterface' of undefined at Function.get queryInterface [as queryInterface] (\node_modules\sequelize\lib\model.js:55:27) at isFunctionMember (\node_modules\sequelize-typescript\dist\model\model\model.js:99:25) at D:\Wizr\Project\Wizr_api\node_modules\sequelize-typescript\dist\model\model\model.js:96:5 at Array.filter (<anonymous>) at Object.<anonymous> (\node_modules\sequelize-typescript\dist\model\model\model.js:95:6) at Module._compile (node:internal/modules/cjs/loader:1083:30) at Module._compile (\node_modules\source-map-support\source-map-support.js:547:25) at Module._extensions..js (node:internal/modules/cjs/loader:1112:10) at Object.nodeDevHook [as .js] (D:\Wizr\Project\Wizr_api\node_modules\ts-node-dev\lib\hook.js:63:13) at Module.load (node:internal/modules/cjs/loader:948:32) [ERROR] 17:32:25 TypeError: Cannot read property 'getQueryInterface' of undefined

Should I just downgrade for the time being?

Version 5.22.3 works fine at the moment.

ts-node version ^9.0.0
typescript version ^4.0.5
node version 15.0.1
sequelize version 6.3.5
sequelize-typescript version ^1.1.0

The error still persists in version 6.3.5

TypeError: Cannot read property 'getQueryInterface' of undefined at Function.get queryInterface [as queryInterface] (\node_modules\sequelize\lib\model.js:55:27) at isFunctionMember (\node_modules\sequelize-typescript\dist\model\model\model.js:99:25) at D:\Wizr\Project\Wizr_api\node_modules\sequelize-typescript\dist\model\model\model.js:96:5 at Array.filter (<anonymous>) at Object.<anonymous> (\node_modules\sequelize-typescript\dist\model\model\model.js:95:6) at Module._compile (node:internal/modules/cjs/loader:1083:30) at Module._compile (\node_modules\source-map-support\source-map-support.js:547:25) at Module._extensions..js (node:internal/modules/cjs/loader:1112:10) at Object.nodeDevHook [as .js] (D:\Wizr\Project\Wizr_api\node_modules\ts-node-dev\lib\hook.js:63:13) at Module.load (node:internal/modules/cjs/loader:948:32) [ERROR] 17:32:25 TypeError: Cannot read property 'getQueryInterface' of undefined

Should I just downgrade for the time being?

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

Yes, I am also get this error. And resolve when downgrading [email protected] version.
npm install [email protected] --save

resolved by downgrading to [email protected]

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: https://github.com/RobinBuschmann/sequelize-typescript/issues/805#issuecomment-693391724

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

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lilling picture lilling  路  4Comments

josecolella picture josecolella  路  4Comments

chanlito picture chanlito  路  3Comments

samanmohamadi picture samanmohamadi  路  5Comments

ReneHollander picture ReneHollander  路  3Comments