sequelize多表联合查询怎么写?

3个表格怎样联合查询?
以下是3个表格

@Table({
  tableName: 'published',
  timestamps: false,
})
export default class Published extends Model<Published> {
  @Column({
    autoIncrement: true,
    primaryKey: true,
    type: DataType.INTEGER,
  })
  public id: number;

  @ForeignKey(() => Teacher)
  @Column({
    allowNull: false,
    type: DataType.INTEGER,
    unique: true,
  })
  public teacherId: number;

  @ForeignKey(() => Prototype)
  @Column({
    allowNull: false,
    type: DataType.INTEGER,
    unique: true,
  })
  public prototypeId: number;

  @ForeignKey(() => Organization)
  @Column({
    allowNull: false,
    type: DataType.INTEGER,
    unique: true,
  })
  public organizationId: number;

  @BelongsTo(() => Teacher)
  public teacher: Teacher;

  @BelongsTo(() => Prototype)
  public prototype: Prototype;

  @BelongsTo(() => Organization)
  public organization: Organization;

  @Column({
    allowNull: false,
    type: DataType.STRING,
  })
  public name: string;

  @Column({
    allowNull: false,
    type: DataType.TEXT,
  })
  public content: string;
}
@Table({
  tableName: 'organization',
  timestamps: false,
})
export default class Organization extends Model<Organization> {
  @Column({
    autoIncrement: true,
    primaryKey: true,
    type: DataType.INTEGER,
  })
  public id: number;

  @Column({
    allowNull: false,
    type: DataType.INTEGER,
    unique: true,
  })
  public userId: number;

  @Column({
    allowNull: false,
    type: DataType.STRING,
  })
  public name: string;
}
@Table({
  tableName: 'teacher',
  timestamps: false,
})
export default class Teacher extends Model<Teacher> {
  @Column({
    autoIncrement: true,
    primaryKey: true,
    type: DataType.INTEGER,
  })
  public id: number;

  @Column({
    allowNull: false,
    type: DataType.TEXT,
  })
  public intro: string;

  @Column({
    allowNull: false,
    field: 'avatar_url',
    type: DataType.STRING,
  })
  public avatarUrl: string;

  @ForeignKey(() => User)
  @Column({
    allowNull: false,
    field: 'user_id',
    type: DataType.INTEGER,
    unique: true,
  })
  public userId: number;

  @BelongsTo(() => User)
  public user: User;
}

大概是想要这种效果,到底该怎么写

const query={
    include: [
    {
        model: Organization, 
        as: 'organization1',
        where:{id:Sequelize.col('Published.organizationId')},
        attributes:['id','name']
    },
    {
        model:Prototype,
        as:'prototype1',
        where:{id:Sequelize.col('Published.prototypeId')},
        attributes:['id','name','content','teacherId']
    }
    ],
    where:{id:id}
  }
  const published = await Published.findOne(query);
  if (published === null) {
    throw PUBLISHED_NOT_FOUND;
  }
  return published;

调用返回值published.organization1时提示不存在属性,怎样才对?新手求各位大佬解决

阅读 2k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题