Sequelize-typescript: 'isInitialized' of undefined association problem.

Created on 15 Apr 2018  ·  14Comments  ·  Source: RobinBuschmann/sequelize-typescript

Looks like that when we try to put an association of model B.model.ts on an A.model.ts results in the following error:

TypeError: Cannot read property 'isInitialized' of undefined

/node_modules/sequelize-typescript/lib/models/association/BaseAssociation.js:19
        if (!modelClass.isInitialized) {
                        ^
TypeError: Cannot read property 'isInitialized' of undefined
    at BelongsToAssociation.BaseAssociation.getAssociatedClass (/node_modules/sequelize-typescript/lib/models/association/BaseAssociation.js:19:25)

The file os model names must be ordered by his association as well ?

Most helpful comment

I just ran into the same issue. Thanks @Mreddi1 for pointing me in the right direction. I just want to add a little info for the next person who comes here looking for answers. In my case I was importing the association via import { AssociatedModel } from "../models" where models is a module that imports and reexports all my models. The fix for me was to use import { AssociatedModel } from "./AssociatedModel". Guess it's something about the indirect import?

All 14 comments

Hey @aasmm7x can you provide a more advanced example or a github repo which reproduces the issue?

@aasmm7x Do you still have this issue?

Hi @RobinBuschmann
I am having same issue , i have to model classes -
1) In Questions.ts --

 @AllowNull(false)
    @ForeignKey(() => Options)
    @Column({
      unique: true,
    })
    public optionId: number;

    @BelongsTo(() => Options)
    public options: Options;

and 2) in Options.ts

 @AllowNull(false)
    @ForeignKey(() => Question)
    @Column({
      unique: true,
    })
    public questionId: number;

    @HasOne(() => Question)
    question: Question;

when i am starting me server getting error as below -
\node_modules\sequelize-typescript\lib\models\association\BaseAssociation.js:19
if (!modelClass.isInitialized) {
^

TypeError: Cannot read property 'isInitialized' of undefined
at HasAssociation.BaseAssociation.getAssociatedClass (E:\A_TS_WS\Exceed\node_modules\sequelize-typescript\lib\models\association\BaseAssociation.js:19:24)
at HasAssociation.getPreparedOptions (E:\A_TS_WS\Exceed\node_modules\sequelize-typescript\lib\models\association\HasAssociation.js:35:36)
at HasAssociation.BaseAssociation.init (E:\A_TS_WS\Exceed\node_modules\sequelize-typescript\lib\models\association\BaseAssociation.js:28:34)
at E:\A_TS_WS\Exceed\node_modules\sequelize-typescript\lib\models\BaseSequelize.js:81:29
at Array.forEach (native)
at E:\A_TS_WS\Exceed\node_modules\sequelize-typescript\lib\models\BaseSequelize.js:80:26
at Array.forEach (native)
at Sequelize.BaseSequelize.associateModels (E:\A_TS_WS\Exceed\node_modules\sequelize-typescript\lib\models\BaseSequelize.js:76:16)
at Sequelize.BaseSequelize.addModels (E:\A_TS_WS\Exceed\node_modules\sequelize-typescript\lib\models\BaseSequelize.js:62:14)
at Object. (E:\A_TS_WS\Exceed\server\models\Options.js:70:20)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)

@skshrivas Unfortunately I cannot reproduce the issue. Can you provide an example repo please?

@aasm-moura @skshrivas use:

export default class
or
import {ClassName} from './ClassName'

Close the issue due to no further information was given

I just ran into the same issue. Thanks @Mreddi1 for pointing me in the right direction. I just want to add a little info for the next person who comes here looking for answers. In my case I was importing the association via import { AssociatedModel } from "../models" where models is a module that imports and reexports all my models. The fix for me was to use import { AssociatedModel } from "./AssociatedModel". Guess it's something about the indirect import?

I just ran into the same issue.

I solved The Problem.

Associated Models should be Added To Models at the same time.

It Will Not Work.
sequelize.addModels([ Options]); sequelize.addModels([ Question ]);

It Will Work
sequelize.addModels([ Options, Question ]);

And re export Options, And Question from the file which is 'sequelize.addModels' is called so that models be initialized before imported.

Not solved my problem still facing same issue

@vipulpatel1994 Can you create a new issue with a description of your very problem?

The errorhandling could be better. A wrong import caused my problem.

WHAT AN ISSUE!
Thanks @eboswort! You were absolutely right!

I'm going to expose my problem to future developers.


A Model needed other Models, so:

This is wrong ❌

import { BoundaryModel, CropModel, FieldModel, SeasonModel } from '..';

This is right ✔️

import BoundaryModel from '../boundary/boundary.model';
import CropModel from '../crop/crop.model';
import FieldModel from '../field/field.model';
import SeasonModel from '../season/season.model';

You can not import models using a module that re-exports those models, I don't know why

If a Model needs another Model, import the second Model using the default export

Was this page helpful?
0 / 5 - 0 ratings