Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModelNotInitializedError: Model not initialized: Member "create" cannot be called [Model] needs to be added to a Sequelize instance. #1717

Open
JoshLeez opened this issue Mar 31, 2024 · 0 comments

Comments

@JoshLeez
Copy link

Versions

  • sequelize: 6.37.1
  • sequelize-typescript: 2.1.6
  • typescript: 5
  • I'm submitting a ...

[X] bug report
[ ] feature request

Issue
ModelNotInitializedError: Model not initialized: Member "create" cannot be called. "Todo" needs to be added to a Sequelize instance.

Context
I have 1 simple model that is Todo with name of the file Todo.model.ts.

import {Table, Column, Model, DataType, BeforeUpdate} from 'sequelize-typescript';

@Table({
    timestamps: true,
    tableName: "todos",
    modelName: "Todo",
})

export default class Todo extends Model{
    @Column
       declare list: string; 
    
}       

And this is my Sequelize Configuration.

import { Sequelize } from "sequelize-typescript";
import  Todo  from "./models/Todo.model";


const database = new Sequelize({
    host : process.env.DB_HOST,
    database: process.env.DB_NAME,
    dialect: 'mysql',
    username: process.env.DB_USER,
    password: process.env.DB_PASS,   
    models: [Todo], 
    sync: { force: true },
});


(async () => {
  try {
     await database.authenticate();
     console.log('Database is connected');
     database.addModels([Todo])
     await database.sync({ alter: true, force: true });
     console.log('Models synchronized successfully.');
      await Todo.create({
      list: 'My Todo List',
    });
   } catch (err) {
      console.error('Error during database setup:', err);
   }
 })();

 export default database

everytime if I try to make an api call it will always the error ModelNotInitializedError: Model not initialized: Member "create" cannot be called. "Todo" needs to be added to a Sequelize instance. And I don't know why.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant