sequelize / sequelize-typescript

Decorators and some other features for sequelize
MIT License
2.78k stars 280 forks source link

Sequelize-typescript cannot use include in query #572

Closed fccruz closed 5 years ago

fccruz commented 5 years ago

Hi,

Every time I try to get a simple query, I get the next error

TypeError: Cannot read property 'include' of undefined at Model.get (/admin/node_modules/sequelize/lib/model.js:3248:66) at Model.toJSON (/admin/node_modules/sequelize/lib/model.js:4101:12) at JSON.stringify () at stringify (/admin/node_modules/fast-safe-stringify/index.js:11:18) at Format.module.exports.format [as transform] (/admin/node_modules/logform/json.js:24:19) at Format.info [as transform] (/admin/node_modules/logform/combine.js:20:24) at DerivedLogger._transform (/admin/node_modules/winston/lib/winston/logger.js:305:29) at DerivedLogger.Transform._read (/admin/node_modules/winston/node_modules/readable-stream/lib/_stream_transform.js:177:10) at DerivedLogger.Transform._write (/admin/node_modules/winston/node_modules/readable-stream/lib/_stream_transform.js:164:83) at doWrite (/admin/node_modules/winston/node_modules/readable-stream/lib/_stream_writable.js:405:139) at writeOrBuffer (/admin/node_modules/winston/node_modules/readable-stream/lib/_stream_writable.js:394:5) at DerivedLogger.Writable.write (/admin/node_modules/winston/node_modules/readable-stream/lib/_stream_writable.js:303:11) at DerivedLogger.log (/admin/node_modules/winston/lib/winston/logger.js:244:14) at DerivedLogger.(anonymous function) (/admin/node_modules/winston/lib/winston/create-logger.js:95:19) at Sequelize.log (/admin/node_modules/sequelize/lib/sequelize.js:1091:23) at Query.run (/admin/node_modules/sequelize/lib/dialects/mysql/query.js:52:22) at Promise.try.then.connection (/admin/node_modules/sequelize/lib/sequelize.js:558:20) at tryCatcher (/admin/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/admin/node_modules/bluebird/js/release/promise.js:512:31) at Promise._settlePromise (/admin/node_modules/bluebird/js/release/promise.js:569:18) at Promise._settlePromise0 (/admin/node_modules/bluebird/js/release/promise.js:614:10) at Promise._settlePromises (/admin/node_modules/bluebird/js/release/promise.js:694:18)

these are the models

import { Model, PrimaryKey, AllowNull, Column, DataType, CreatedAt, UpdatedAt, Table, HasOne } from "sequelize-typescript";
import AdminUser from "../models/AdminUser";

@Table({ tableName: 'establishment', paranoid: false, timestamps: true })
export default class IEstablishment extends Model<IEstablishment> {
  @PrimaryKey
  @AllowNull(false)
  @Column(DataType.INTEGER)
  id : number;

  @CreatedAt
  @AllowNull(false)
  @Column(DataType.DATE)
  created_at: Date; 

  @AllowNull(false)
  @Column(DataType.INTEGER)
  status: number;

  @UpdatedAt
  @AllowNull(false)
  @Column(DataType.DATE)
  updated_at: Date;

  @AllowNull(false)
  @Column(DataType.STRING(100))
  name: string;

  @HasOne(() => AdminUser)
  adminUSer: AdminUser
}
import { Model, Table, PrimaryKey, AutoIncrement, Unique, AllowNull, Column, DataType, CreatedAt, DeletedAt, UpdatedAt, ForeignKey, BelongsTo } from "sequelize-typescript";
import Establishment from "../models/Establishment";

@Table({ paranoid: true, timestamps: true, tableName: 'admin_user' })
export default class IAdminUser extends Model<IAdminUser> {
  @PrimaryKey
  @AutoIncrement
  @Unique
  @AllowNull(false)
  @Column(DataType.INTEGER)
  id: number;

  @Unique
  @AllowNull(false)
  @Column(DataType.STRING(200))
  email: string;

  @AllowNull(false)
  @Column(DataType.STRING(150))
  password: string;

  @CreatedAt
  @Column(DataType.DATE)
  created_at: Date;

  @DeletedAt
  @Column(DataType.DATE)
  deleted_at: Date;

  @UpdatedAt
  @Column(DataType.DATE)
  updated_at: Date

  @ForeignKey(() => Establishment)
  @Column(DataType.INTEGER)
  establishment_id: number;

  @BelongsTo(() => Establishment)
  establishment: Establishment;
}
import IAdminUser from "../db_interfaces/IAdminUser";
import Establishment from "./Establishment";

export default class AdminUser extends IAdminUser {
  static async findUserById(id: number) {
    return await this.findByPrimary(id, {
      include: [Establishment]
    });
  };
}
import IEstablishment from "../db_interfaces/IEstablishment";

export default class Establishment extends IEstablishment {

}

and this is the query

try {
    let user = await AdminUser.findUserById(1);
    return res.send(user);
  } 
  catch(e) {
    logger.error(e);
    return cb(e);
  }
});

Edit: Also when i run the query without the include key it works normally

Thank you in advance

RobinBuschmann commented 5 years ago

@fccruz Can you provide an example repo, which reproduces the issue? I couldn't.

typeofweb commented 5 years ago

@fccruz try return res.send(user.toJSON());

morrigan commented 5 years ago

Did you manage to solve the problem? I have the same issue.

RobinBuschmann commented 5 years ago

@morrigan Can you provide an example repo which reproduces the issue?

typeofweb commented 5 years ago

@morrigan did you try return res.send(user.toJSON()); ? I had exactly the same problem and managed to avoid it simply by running toJSON() on the instance first.

morrigan commented 5 years ago

@RobinBuschmann https://github.com/morrigan/mythx-vulnerability-monitor/blob/master/src/Models/User.ts

@mmiszy yes, same thing

OrenSchwartz commented 5 years ago

happens to me on the latest version after doing toJSON or it equivalent get({plain: true})

RobinBuschmann commented 5 years ago

@morrigan @OrenSchwartz I've released a new beta version (npm i sequelize-typescript@0.6.11-beta.1) which should fix at least the issue morrigan had. Can you confirm?

AnkhHeart commented 5 years ago

@RobinBuschmann I was having the same issue, testing the beta now. I'll let you know the results in the next 24 hours :)

AnkhHeart commented 5 years ago

@RobinBuschmann Fix seems to be good, no exceptions in the past 29 hours of using it :) , thanks.

OrenSchwartz commented 5 years ago

fix seems to be good.

RobinBuschmann commented 5 years ago

I'm closing this, since the fix was released with sequelize-typescript@0.6.11