question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Error: Model not initialized: "Model" needs to be added to a Sequelize instance before "call" can be called."

See original GitHub issue

Hello Typescript-Sequelize’rs, This may be a noob question but I will try to explain as best I can. As you can see, I run into an error when trying to save data to my PostgreSQL database. I tried to reduce everything back to basics to trace the error, here’s my situation.

  1. I set up the sequelize connection to my DB:
const sequelize =  new Sequelize({
        database: dbconfig.database,
        dialect: dbconfig.dialect,
        username: dbconfig.username,
        password: dbconfig.password,
        host: dbconfig.host,
        port: dbconfig.port
});
  1. I confirm that the connection is established using:
sequelize.databaseVersion().then((databaseVersion) => {
    console.log(databaseVersion);
} );

This gives me the correct version of my PostgreSQL DB (10.3 - Ubuntu) as expected.

  1. I created a Person.model.ts and added it to Sequelize using sequelize.addModels([__dirname + '../models/person.model.ts']); The person.model.ts is a copy of the model in your Wiki page.

  2. I then try to create a new instance of the Person using, const person = new Person({name: 'bob', age: 99});

  3. When I try to run the code, I get the following error,

/home/mick/Documents/code/travelbudget/node_modules/sequelize-typescript/lib/models/BaseModel.js:64
                throw new ModelNotInitializedError_1.ModelNotInitializedError(model, { accessedPropertyKey: propertyKey });
                ^

Error: Model not initialized: "Model" needs to be added to a Sequelize instance before "call" can be called.
    at new ModelNotInitializedError (/home/mick/Documents/code/travelbudget/node_modules/sequelize-typescript/lib/models/errors/ModelNotInitializedError.js:18:16)
    at checkInitialization (/home/mick/Documents/code/travelbudget/node_modules/sequelize-typescript/lib/models/BaseModel.js:64:23)
    at Function._target.(anonymous function) [as call] (/home/mick/Documents/code/travelbudget/node_modules/sequelize-typescript/lib/models/BaseModel.js:34:21)
    at new Person (/home/mick/Documents/code/travelbudget/dist/out-tsc/models/person.model.js:26:28)
<snip>
  1. I wasn’t sure if the table needed to exist on my DB, so I created the Person table on the DB.

I then decided to try a different approach, and instead of using the person.model.ts, I created the table directly through Sequelize and added the data that way. This way worked as expected.

const myperson = sequelize.define('person', {
    name: {
      type: Sequelize.STRING
    },
    age: {
      type: Sequelize.REAL
    }
  });

myperson
    .sync({ force: true })
    .then(() => {        
        console.log('Connection synced')
        return mycurrency.create({
            name: 'joe',
            age: '5'
        })
    })
    .catch(err => {
        console.log('err');
    });

My question is, what am I doing wrong that I cannot use the person.model.ts file to create and add new data?

Any help or directions would be greatly appreciated. Thanks!!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
RobinBuschmanncommented, May 17, 2018

An issue that I’ve seen very often is, that the model ist references correctly in one file and in another one the case sensitivity of the used path differs from the one where the model was added:

// index.ts
import {Person} from './person.model';
...
sequelize.addModels([Person]);
// usage.ts
import {Person} from './Person.model'; // <-- Please note capitalized Person here
new Person(...);

Node loads the file content again, so that:

// usage.ts
import {Person as CapitalizedPerson} from './Person.model'; // capital P
import {Person} from './person.model';

Person === CapitalizedPerson // false
2reactions
RobinBuschmanncommented, May 17, 2018

The constructor you’re defined is wrong… see https://github.com/RobinBuschmann/sequelize-typescript/blob/master/lib/models/Model.d.ts#L39

But this is not the actual issue. Did you add the model and try to create the instance of Person in the same file?

For me it is working perfectly well: I’ve created an example repo reflecting what you’ve done: https://github.com/RobinBuschmann/st-366

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Error: Model not initialized: "Model" needs to be added to a ...
"Error: Model not initialized: "Model" needs to be added to a Sequelize instance before "call" can be called." #366.
Read more >
"Model" needs to be added t o a Sequelize instance before ...
Error: Model not initialized: "Model" needs to be added to a Sequelize instance before "call" can be called. typescript · model · initialization ......
Read more >
Model Instances - Sequelize
As you already know, a model is an ES6 class. An instance of the class represents one object from that model (which maps...
Read more >
Sequelize ModelNotInitializedError, Object need to be added ...
You need to remove cross-references from model modules and define functions to register associations and call them after all your models will be...
Read more >
How To Use Sequelize with Node.js and MySQL - DigitalOcean
Likewise, if you need to set such a value, you can use defaultValue: "value" . Next, you'll add the book model to your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found