sequelize-typescript icon indicating copy to clipboard operation
sequelize-typescript copied to clipboard

Cannot read property 'getQueryInterface' of undefined

Open MalavitaC opened this issue 4 years ago • 27 comments

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

MalavitaC avatar Jul 01 '20 10:07 MalavitaC

I solved it by downgrading sequelize to 5.21.10

cibergarri avatar Jul 05 '20 21:07 cibergarri

Same problem here

bianchi avatar Jul 14 '20 23:07 bianchi

Also solved by downgrading sequelize (this time to 5.22.3)

kisamoto avatar Jul 23 '20 13:07 kisamoto

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.

asnaseer-resilient avatar Aug 17 '20 13:08 asnaseer-resilient

Same

demfabris avatar Aug 19 '20 05:08 demfabris

same problem here ):

veronesecoms avatar Aug 20 '20 23:08 veronesecoms

same

pobidowski avatar Aug 28 '20 13:08 pobidowski

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.

mistic100 avatar Aug 28 '20 19:08 mistic100

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

vjpr avatar Sep 16 '20 13:09 vjpr

same

xyunsh avatar Nov 01 '20 09:11 xyunsh

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?

sivansundar avatar Nov 02 '20 12:11 sivansundar

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?

sivansundar avatar Nov 02 '20 12:11 sivansundar

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

nerdophile avatar Nov 17 '20 08:11 nerdophile

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

nguyenvantien2009 avatar Dec 01 '20 06:12 nguyenvantien2009

resolved by downgrading to [email protected]

kiwenlau avatar Dec 16 '20 11:12 kiwenlau

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

vjpr avatar Dec 16 '20 14:12 vjpr

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

guoliang avatar Dec 21 '20 14:12 guoliang

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.

russocorp avatar Mar 06 '21 20:03 russocorp

Issue still persist even with sequelize-typescript@next, or npm:@vjpr/sequelize-typescript@^1.1.0-vjpr.0 "sequelize": "^6.6.2",

edgar0011 avatar Apr 07 '21 09:04 edgar0011

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.

Hey @russocorp,

Can you please elaborate it more?

diwakarS avatar Apr 19 '21 05:04 diwakarS

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.

Hey @russocorp,

Can you please elaborate it more?

What he/she means, I think, is that before running or instanciating a model that has a relationship with other models, make sure its dependents are ran first.

In the example below, OrderProduct (which is a pivot table that has references to both order and product) will throw an error Cannot read property 'getQueryInterface' of undefined, because its dependencies have not yet run.

const Order = OrderFactory(sequelize, datatypes)
const OrderProduct = OrderProductFactory(sequelize, datatypes)
const Product = ProductFactory(sequelize, datatypes)

To fix, just change the order. This way, the dependencies of OrderProduct run, and it will self will run the last:

const Order = OrderFactory(sequelize, datatypes)
const Product = ProductFactory(sequelize, datatypes)
const OrderProduct = OrderProductFactory(sequelize, datatypes)

RTAndrew avatar May 22 '21 00:05 RTAndrew

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",

elirandav avatar Jun 23 '21 12:06 elirandav

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.

Hey @russocorp, Can you please elaborate it more?

What he/she means, I think, is that before running or instanciating a model that has a relationship with other models, make sure its dependents are ran first.

In the example below, OrderProduct (which is a pivot table that has references to both order and product) will throw an error Cannot read property 'getQueryInterface' of undefined, because its dependencies have not yet run.

const Order = OrderFactory(sequelize, datatypes)
const OrderProduct = OrderProductFactory(sequelize, datatypes)
const Product = ProductFactory(sequelize, datatypes)

To fix, just change the order. This way, the dependencies of OrderProduct run, and it will self will run the last:

const Order = OrderFactory(sequelize, datatypes)
const Product = ProductFactory(sequelize, datatypes)
const OrderProduct = OrderProductFactory(sequelize, datatypes)

Yes, that's exactly what happened. Sorry for the delay in replying, I had not seen the emails.

russocorp avatar Jun 23 '21 12:06 russocorp

I don't have any established relationships, and I get this error no matter what version of sequelize I try. 5.22.3, 5.22.1, and any of the 5.21 branches

patrickehansen avatar Jun 29 '21 03:06 patrickehansen

I've basically been flopping back and forth between this error and TypeError: this.lib.Database is not a constructor regardless of what versions of either sequelize (5.21.3, 5.21.10, 5.22.1, 5.22.3, ^6.5.0) or sequelize-typescript (1.0.0, 1.1.0, 2.1.0) I used. Is it possible this is just fundamentally incompatible with sequelize-mock/sequelize-mock-v5 being included? Like can I just NOT mock models that way if I've used types and decorators to build and configure my models? Is that written anywhere or is there a reason I should know that?

pandrews-hdai avatar Jun 30 '21 20:06 pandrews-hdai

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",

This worked for me as well. Thanks

luciferankon avatar Oct 01 '21 04:10 luciferankon

You might not need to downgrade the package, watch what your passing to the reference field, its supposed to be a reference to another table name, that why you get this error.

its a TYPERROR

iamgabrielsoft avatar Dec 29 '23 08:12 iamgabrielsoft