Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'getQueryInterface' of undefined #805

Open
MalavitaC opened this issue Jul 1, 2020 · 27 comments
Open

Cannot read property 'getQueryInterface' of undefined #805

MalavitaC opened this issue Jul 1, 2020 · 27 comments

Comments

@MalavitaC
Copy link

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
@cibergarri
Copy link

I solved it by downgrading sequelize to 5.21.10

@bianchi
Copy link

bianchi commented Jul 14, 2020

Same problem here

@kisamoto
Copy link

kisamoto commented Jul 23, 2020

Also solved by downgrading sequelize (this time to 5.22.3)

@asnaseer-resilient
Copy link

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.

@demfabris
Copy link

Same

@veronesecoms
Copy link

same problem here ):

@pobidowski
Copy link

same

@mistic100
Copy link

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.

@vjpr
Copy link

vjpr commented Sep 16, 2020

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

okpalaChidiebere added a commit to okpalaChidiebere/image-filter-code that referenced this issue Oct 14, 2020
to query to our provisioned server to get all the images that has been filtered by users

From the previous commit, the endpoint added was to download an image from a public url. We also upload the image to our server through signed url. We set our S3 bucket to be secure from our class note

FYI:*****
Fixed the last bug from the last commit by upgrading the transpile target to js file from es5 to es6 in the tconfig file from the help from this link sequelize/sequelize#11270  **
sequelize/sequelize-typescript#366 some other error i encountered but was minor

Another thing to have in mind was when i download the latest sequelize package of 6.3.5, i get an error when i call the addModels() method. But i solve the issue by following the link sequelize/sequelize-typescript#805  However, you will have to set your compiler to still compile even though it will still show a syntax error at the function argument. Its all good ;)
@xyunsh
Copy link

xyunsh commented Nov 1, 2020

same

@sivansundar
Copy link

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
Copy link

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?

@nerdophile
Copy link

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

@nguyenvantien2009
Copy link

nguyenvantien2009 commented Dec 1, 2020

Yes, I am also get this error. And resolve when downgrading sequelize@5.22.3 version.
npm install sequelize@5.22.3 --save

@kiwenlau
Copy link

kiwenlau commented Dec 16, 2020

resolved by downgrading to sequelize@5.22.3

@vjpr
Copy link

vjpr commented Dec 16, 2020

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)

@guoliang
Copy link

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

@russocorp
Copy link

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.

@edgar0011
Copy link

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

@diwakarS
Copy link

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?

@RTAndrew
Copy link

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)

@elirandav
Copy link

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

@russocorp
Copy link

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.

@patrickehansen
Copy link

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 patrickehansen mentioned this issue Jun 29, 2021
2 tasks
@pandrews-hdai
Copy link

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?

@luciferankon
Copy link

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

chaeinP pushed a commit to chaeinP/42checkin_server that referenced this issue Jan 25, 2022
@iamgabrielsoft
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests