sequelize-typescript icon indicating copy to clipboard operation
sequelize-typescript copied to clipboard

Encountered ModelNotInitializedError when trying to build model object.

Open jamesdeath123 opened this issue 2 years ago • 1 comments

I have the following model infrastructure/model/Person.ts:

type PersonAttributes = {
  id: string;
  name: string;
};

type PersonCreationAttributes = Optional<PersonAttributes, 'id'>;

@Table({ tableName: 'alarms' })
class Person extends Model<PersonAttributes, PersonCreationAttributes> {
  @PrimaryKey
  @AllowNull(false)
  @Index
  @Column
  id: string;

  @AllowNull(false)
  @Column
  name: string;
}

export default Person;

And then I am using it as:


import Person from '@infrastructure/model/Person';

const DBInstance = new Sequelize(config.db.database, config.db.username, config.db.password, {
  host: config.db.host,
  dialect: config.db.dialect as Dialect,
  repositoryMode: true,
  logging: config.db.debug,
});

DBInstance.addModels([Person]);

Person.build({
  id: "111",
  name: '111',
});

export default DBInstance;

which gives me the error:

ModelNotInitializedError: Model not initialized: Member "build" cannot be called. "Person" needs to be added to a Sequelize instance.

Please advise. Versions:

  • sequelize: 6.31.0
  • sequelize-typescript: 2.1.5

Issue type

  • [x] bug report
  • [ ] feature request

jamesdeath123 avatar Apr 27 '23 17:04 jamesdeath123

I am having similar issue. seems that is a frequent appearing error in this library.

youssiffouad avatar Mar 02 '25 12:03 youssiffouad