NestJs Sequalizer Mysql issue with registering my model

Viewed 921

I'm using NestJs and mysql and sequalize. and here is mysql database module providers file .


import { Sequelize } from 'sequelize-typescript';
import { PostModel } from '../post/post.model';

export const databaseProviders = [
  {
    provide: 'SEQUELIZE',
    useFactory: async () => {
      const sequelize = new Sequelize({
        dialect: 'mysql',
        host: 'localhost',
        port: 3306,
        username: 'root',
        password: '123456',
        database: 'nest',
      });
      sequelize.addModels([PostModel]);
      await sequelize.sync();
      return sequelize;
    },
  },
];

and here is my post model code :


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

@Table
export class PostModel extends Model<PostModel> {
  @Column
  title: string;

  @Column
  description: string;

  @Column
  user: string;
}

I want to register my model and It gives me following error :

src/database/database.providers.ts:17:28 - error TS2769: No overload matches this call.
  The last overload gave the following error.
    Type 'typeof PostModel' is not assignable to type 'string | ModelCtor<Model<any, any>>'.
      Type 'typeof PostModel' is not assignable to type 'ModelCtor<Model<any, any>>'.
        Type 'typeof PostModel' is not assignable to type 'typeof Model'.
          Construct signature return types 'PostModel' and 'Model<T, T2>' are incompatible.
            The types of '_attributes' are incompatible between these types.
              Type 'PostModel' is not assignable to type 'T'.
                'T' could be instantiated with an arbitrary type which could be unrelated to 'PostModel'.

17       sequelize.addModels([PostModel]);

I think I did everything based on official documents which resides here : https://docs.nestjs.com/recipes/sql-sequelize

and finally this is my package.json if you want to see packages versions.

{
  "name": "nest-js",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^7.0.0",
    "@nestjs/core": "^7.0.0",
    "@nestjs/platform-express": "^7.0.0",
    "mysql2": "^2.2.5",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.5.4",
    "sequelize": "^6.3.5",
    "sequelize-typescript": "^1.1.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^7.0.0",
    "@nestjs/schematics": "^7.0.0",
    "@nestjs/testing": "^7.0.0",
    "@types/express": "^4.17.3",
    "@types/jest": "26.0.10",
    "@types/node": "^13.9.1",
    "@types/sequelize": "^4.28.9",
    "@types/supertest": "^2.0.8",
    "@typescript-eslint/eslint-plugin": "3.9.1",
    "@typescript-eslint/parser": "3.9.1",
    "eslint": "7.7.0",
    "eslint-config-prettier": "^6.10.0",
    "eslint-plugin-import": "^2.20.1",
    "jest": "26.4.2",
    "prettier": "^1.19.1",
    "supertest": "^4.0.2",
    "ts-jest": "26.2.0",
    "ts-loader": "^6.2.1",
    "ts-node": "9.0.0",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^3.7.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

2 Answers

Hy, in order to fix it, try to change export class PostModel extends Model<PostModel> to export class PostModel extends Model

change

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

@Table
export class PostModel extends Model<PostModel> {
  @Column
  title: string;

  @Column
  description: string;

  @Column
  user: string;
}

to

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

@Table
export class PostModel extends Model {
  @Column
  title: string;

  @Column
  description: string;

  @Column
  user: string;
}
Related