sequelize-typescript
sequelize-typescript copied to clipboard
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
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.
Issue still persist even with sequelize-typescript@next, or npm:@vjpr/sequelize-typescript@^1.1.0-vjpr.0 "sequelize": "^6.6.2",
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?
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)
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",
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.
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
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?
I was able to resolve that by upgrading also packages
sequelize-cli
andsequelize-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
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