profile
viewpoint

Ask questionsModel not initialized - addModels

Here's a reproduction: https://github.com/kamilkisiela/sequelize-init


I have two models, a oneToMany relation. I read #366 and what I get is:

Model not initialized: "Message" needs to be added to a Sequelize instance before association can be resolved

import { Message } from './models/message';
import { User } from './models/user';
import { Sequelize } from 'sequelize-typescript';

const sequelize = new Sequelize({...});

sequelize.addModels([User, Message]);
// Message.ts

@Table
export class Message extends Model<Message> {
  @BelongsTo(() => User, {
    foreignKey: 'senderId',
    targetKey: 'id',
  })
  sender: User;

  @Column
  content: string;
}
// User.ts
@Table
export class User extends Model<User> {
  @Column
  username: string;

  @HasMany(() => Message, {
    foreignKey: 'senderId',
    sourceKey: 'id',
  })
  senderMessages: Message[];
}
RobinBuschmann/sequelize-typescript

Answer questions adminparry

Error: Model not initialized: "Author" needs to be added to a Sequelize instance before association can be resolved.

useful!
source:https://uonfu.com/
Github User Rank List