Sequelize-typescript: TypeError: Class constructor Model cannot be invoked without 'new'

Created on 26 Feb 2018  路  10Comments  路  Source: RobinBuschmann/sequelize-typescript

I'm trying to use sequelize-typescript like this:

import { Model, Table, Column, Sequelize } from 'sequelize-typescript';

@Table
class User extends Model<User> {
  @Column name: string;
  @Column age: number;
}

const seq = new Sequelize({
  database: '../database.db',
  username: 'root',
  password: '',
  dialect: 'sqlite',
  storage: '../database.db'
});
seq.addModels([User]);

const user = new User({ 
  name: 'oliver twist',
  age: 14
});
user.save();

But its throwing a really weird error. It's saying that Class constructor Model cannot be invoked without 'new' and then it point at some random part of the code, that has nothing to do with sequelize-typescript, and its a different part each time i make a change to the code (no matter how small of a change). But the error goes away if I remove the call to new User. Why is this?
Also, i can't init User with User.build or User.create as the README would suggest, there simply isn't a build or create method on the User object...

__Versions:__

  • sequelize-typescript: 0.6.2
  • sequelize: 4.34.0
  • sqlite3: 3.1.13
  • node: 9.4.0
  • typescript: 2.7.2
  • ts-node: 5.0.0
  • reflect-metadata: 0.1.12

__tsconfig.json__

{
  "compilerOptions": {
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "lib": [ "ES2015" ],                             /* Specify library files to be included in the compilation. */
    "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "esModuleInterop": true,                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
    "emitDecoratorMetadata": true          /* Enables experimental support for emitting type metadata for decorators. */
  }
}

__Update1:__

After forcefully trying to use create, it turns out that it was a problem with the vscode instellisense, and the create method does exist.

User.create({
  name: 'oliver twist',
  age: 14
});

However the error remains...

__Update2:__

After some more searching i found issue https://github.com/RobinBuschmann/sequelize-typescript/issues/26 which solved this problem.
However now I'm getting Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such table: User

__Update3:__

Turns out I had to put the create call inside of an anonymous async function call.

(async () => {
  User.create({
    name: 'oliver twist',
    age: 14
  });
  const allUsers = await User.findAll();
  console.log(allUsers.map(u => u.name));
})();

In case this is intended, this really needs to be communicated better in the README...
If it's not intended, then how am i supporsed to use it?

Most helpful comment

Hey @Olian04, you need to set target at least to es6 in tsconfig.json

This is because es5 classes cannot inherit es6 classes, see here https://stackoverflow.com/questions/30689817/es6-call-class-constructor-without-new-keyword

All 10 comments

Hey @Olian04, you need to set target at least to es6 in tsconfig.json

This is because es5 classes cannot inherit es6 classes, see here https://stackoverflow.com/questions/30689817/es6-call-class-constructor-without-new-keyword

@RobinBuschmann Thank you, but please read the entire issue.

Are you pointing to the Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such table: User issue? Did you create the table before creating a user?

@RobinBuschmann Shouldn't sequelize-typescript do that for me when i call create on a table object with no table?

No, you need to do this manually by for example running sync on the sequelize instance: await sequelize.sync({force: true})

@RobinBuschmann the problem with that is that 'sync' doesn't exist on Sequelize according to the typings. I get a typescript error when I try to use it.
I have to '@ts-ignore' it.

Hey @Olian04, did you get it working?

@RobinBuschmann yes I did. Sorry I forgot to respond.

No problem :) then I close this issue now

Was this page helpful?
0 / 5 - 0 ratings