Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Model not initialized: "Model" needs to be added t o a Sequelize instance before "call" can be called. (Sequelize-typescript)

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.
like image 715
Karim Mirazul Avatar asked Jan 05 '18 02:01

Karim Mirazul


1 Answers

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

like image 182
Medet Tleukabiluly Avatar answered Sep 19 '22 13:09

Medet Tleukabiluly