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.
const sequelize = new Sequelize({
database: dbconfig.database,
dialect: dbconfig.dialect,
username: dbconfig.username,
password: dbconfig.password,
host: dbconfig.host,
port: dbconfig.port
});
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 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!!
Hey @micklynch, you either need to pass the class reference, the path to the directory(not the actual file) or a glob.
So for example sequelize.addModels([Person]) should do the job :)
Thanks for the fast response @RobinBuschmann.
I tried the following:
import { Person } from '../models/person.model';
...
sequelize.addModels([Person]);
const person = new Person({name: 'bob', age: 99});
And I still get the error.
Here's my `Person.model.ts' file
import {Table, Column, Model, CreatedAt, UpdatedAt} from 'sequelize-typescript';
@Table
export class Person extends Model<Person> {
constructor(name?: any, age?: any) {
super(name, age);
console.log(name);
}
@Column
name: string;
@Column
age: number;
}
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
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
Thanks a million Robin.
I think it was the constructor that was causing the issue. After I fixed that, I got another error that was related to my tsconfig.json. The repo you created for me was very helpful is sorting out that problem.
Cheers and keep up the great work!!
You're welcome :)
Then I'll close this issue now
Most helpful comment
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: