Author.ts
import {Table, Model, Column, DataType} from 'sequelize-typescript'
@Table
export class Author extends Model<Author> {
constructor(){
super();
}
@Column(DataType.STRING)
fname: string
@Column(DataType.STRING)
lname: string
}
App.ts
import {Sequelize} from 'sequelize-typescript';
import { customer } from './model/customer';
import { Author } from './model/Author';
export class SQLRepo {
repo:Sequelize = null;
constructor(){
this.connectDb();
}
connectDb(): any {
this.repo = new Sequelize({
database: 'postgres',
dialect: 'postgres',
host: 'localhost',
username: 'postgres',
password: 'xxxxx',
storage:
'D:/Sequalize/assosiation/database.sqlite',
modelPaths: [__dirname + '/models'],
define: {
underscored: false,
freezeTableName: false,
timestamps: false
}
});
this.repo.addModels([Author])
this.repo.
authenticate().
then(function(){
console.log("database connected ...")
new Author({fname: 'karim' , lname: 'mirazul'}).save();
}).
catch(function(error){
console.log("Database catch block : "+ error)
})
}
}
new SQLRepo();
create SQLRepo call object so by this I can connect to database successfully but It shows error after database is connected .. When it try to insert data into Author
it give me this output :
Executing (default): SELECT 1+1 AS result database connected ... Database catch block :
Error: Model not initialized: "Model" needs to be added to
a Sequelize instance before "call" can be called.
Remove constructor call from model
import {Table, Model, Column, DataType} from 'sequelize-typescript'
@Table
export class Author extends Model<Author> {
// constructor(){
// super();
// }
@Column(DataType.STRING)
fname: string
@Column(DataType.STRING)
lname: string
}
You cannot use constructor as it's used by sequelize for internal things
source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With