sequelize / sequelize-typescript

Decorators and some other features for sequelize

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Encountered ModelNotInitializedError when trying to build model object.

jamesdeath123 opened this issue · 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

  • bug report
  • feature request