"Error: Model not initialized: "Model" needs to be added to a Sequelize instance before "call" can be called."
See original GitHub issueHello 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.
- 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
});
- 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.
-
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. -
I then try to create a new instance of the Person using,
const person = new Person({name: 'bob', age: 99});
-
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>
- 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:
- Created 5 years ago
- Reactions:6
- Comments:9 (4 by maintainers)
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:
Node loads the file content again, so that:
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