sequelize-typescript icon indicating copy to clipboard operation
sequelize-typescript copied to clipboard

Error: Model not initialized: User cannot be instantiated. "User" needs to be added to a Sequelize instance.

Open anaSDCorreia opened this issue 3 years ago • 3 comments

Hello everyone, I'm using meteor with postgres db.

Issue

Error: Model not initialized: User cannot be instantiated. "User" needs to be added to a Sequelize instance.

Versions

  • sequelize - "^6.16.1":
  • sequelize-typescript - "^2.1.3":
  • typescript - "^4.4.4":

Issue type

  • [x] bug report
  • [ ] feature request

Actual behavior

I'm using meteor with postgres db. I created the User class with the name field. I initialize Sequelize with models:[User], and it runs smoothly. It creates the User table in the DB if it doesn't exist. The problem is when I try to create a new user, the library returns an error indicating that the User object is not in the Model. When I do console.log(User === this.sequelize.models.User); it returns false.

Expected behavior

It will be possible to create objects of type User without returning errors.

Steps to reproduce

I downloaded the examples from the library's github and the same problem occurred. Just change the initialize from sequelize to Postgres.

export const sequelize = new Sequelize("XX", "XX", "XX",{
  dialect: 'postgres',
  port:  5432,
  host:  "localhost",
  repositoryMode: true,
  logging:console.log,
  models: [User, Post],
});

Related code

import {User} from "/imports/core/model/typing/method";
import {Sequelize} from "sequelize-typescript";

public async connect(config: any)
    {
        this.sequelize = new Sequelize(config.databaseName, config.username, config.password, {
            dialect: 'postgres',
            port:  config.port,
            host:  config.host,
            models:[User],
            repositoryMode: true,
            logging:console.log,
            dialectOptions: {}
        });

await this.sequelize.authenticate()
            .then(() => {
                this.sequelize.sync().then(() => Log.debug("Sucess")).then(() => Log.debug("Sucess")).then(() => {
                    console.log(this.sequelize.models);
                    var u =new User({name:"sdd"});
                    console.log(u.name);
                    console.log(User === this.sequelize.models.User);

                })
            });
    }


tsconfig file

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es2018",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "module": "esNext",
    "lib": ["esnext", "dom"],
    "allowJs": true,
    "checkJs": false,
    "jsx": "preserve",
    "incremental": true,
    "noEmit": true,
    "allowSyntheticDefaultImports": true,
    "allowJs": true,

    /* Strict Type-Checking Options */
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,

    /* Additional Checks */
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": false,
    "noFallthroughCasesInSwitch": false,

    /* Module Resolution Options */
    "baseUrl": ".",
    "paths": {
      /* Support absolute /imports/* with a leading '/' */
      "/*": ["*"]
    },
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "types": ["node", "mocha"],
    "esModuleInterop": true,
    "preserveSymlinks": true
  },
  "exclude": [
    "./.meteor/**",
    "./packages/**"
  ]
}

packge.json

{
  "name": "Phyloviz-Universal",
  "private": true,
  "scripts": {
    "start": "meteor run",
    "test": "meteor test --once --driver-package meteortesting:mocha",
    "test-app": "TEST_WATCH=1 meteor test --full-app --driver-package meteortesting:mocha",
    "visualize": "meteor --production --extra-packages bundle-visualizer"
  },
  "dependencies": {
    "@babel/runtime": "^7.15.4",
    "datastructures-js": "^10.2.0",
    "meteor-node-stubs": "^1.1.0",
    "pg": "^8.7.3",
    "pg-hstore": "^2.3.4",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "reflect-metadata": "^0.1.13",
    "sequelize": "^6.16.1",
    "sequelize-typescript": "^2.1.3",
    "sequency": "^0.19.2"
  },
  "devDependencies": {
    "@types/meteor": "^1.4.81",
    "@types/mocha": "^8.2.3",
    "@types/node": "^17.0.18",
    "@types/react": "^17.0.30",
    "@types/react-dom": "^17.0.9",
    "@types/validator": "^13.7.1",
    "typescript": "^4.4.4"
  },
  "meteor": {
    "mainModule": {
      "client": "client/main.tsx",
      "server": "server/main.ts"
    },
    "testModule": "tests/main.ts"
  }
}

anaSDCorreia avatar Feb 17 '22 19:02 anaSDCorreia

I have the same problem. 'sync' does nothing after tsc build

noih avatar Mar 05 '22 06:03 noih

I have the same problem. 'sync' does nothing after tsc build

solved, models: [path.join(__dirname, './models/**/*.ts')]

change to

models: [path.join(__dirname, './models/**/*')]

after build, the file extension is not .ts

noih avatar Mar 05 '22 06:03 noih

I have the same problem. 'sync' does nothing after tsc build

solved, models: [path.join(__dirname, './models/**/*.ts')]

change to

models: [path.join(__dirname, './models/**/*')]

after build, the file extension is not .ts

it worked but with a slight variation

models: [path.join(__dirname, './models/**/*.*')]

den666 avatar Nov 25 '22 03:11 den666