sequelize / sequelize-typescript

Decorators and some other features for sequelize
MIT License
2.79k stars 282 forks source link

Unable to resolve types of columns in clean project #1071

Open morkeleb opened 3 years ago

morkeleb commented 3 years ago

Issue

I'm testing out sequelize-typescript for data access on a brand new next.js project. However I can't get the type resolution to work at all. I suspect it has something todo with the reflection, so I've modified a bit of the error message to contain more information.

Its probably some mistake I did, but I'm now at a loss of what to do next.

The error I'm getting is

Error: Specified type of property 'name' on target '[object SequelizeInstance:_Project]' getting type 'undefined'
            cannot be automatically resolved to a sequelize data type. Please
            define the data type manually
    at Object.getSequelizeTypeByDesignType (/node_modules/sequelize-typescript/dist/model/shared/model-service.js:64:11)
    at annotate (/node_modules/sequelize-typescript/dist/model/column/column.js:32:44)
    at Column (/node_modules/sequelize-typescript/dist/model/column/column.js:14:9)
    at eval (webpack-internal:///./lib/sequelize/models/Project.ts:13:410)
    at Array.reduce (<anonymous>)
    at _applyDecoratedDescriptor (webpack-internal:///./lib/sequelize/models/Project.ts:13:367)
    at eval (webpack-internal:///./lib/sequelize/models/Project.ts:44:27)
    at Object../lib/sequelize/models/Project.ts (/.next/server/pages/api/projects.js:22:1)
    at __webpack_require__ (/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///./lib/sequelize/models/projects.ts:7:64)

The model class is defined as follows

import { Table, Column, Model, HasMany, HasOne } from "sequelize-typescript";

@Table
export default class Project extends Model {
  @Column
  name: string;
}

Here is my sequelize setup:

const sequelize = new Sequelize({
  database: 'some_db',
  dialect: 'sqlite',
  username: 'root',
  password: '',
  storage: ':memory:',
  models: [__dirname + '/models']
})

my tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

Please note I've extended the error message to include more values of the variables in the executing code.

Versions

Here are my dependencies

  "dependencies": {
    "@babel/core": "7.14.6",
    "@babel/plugin-proposal-decorators": "7.14.5",
    "next": "11.0.1",
    "next-auth": "^3.27.1",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "reflect-metadata": "^0.1.13",
    "sequelize": "6.6.2",
    "sequelize-typescript": "2.1.0"
  },
  "devDependencies": {
    "@types/react": "^17.0.13",
    "prettier": "2.3.2",
    "typescript": "^4.3.5"
  }
fabalchemy commented 3 years ago

Hi,

I'm facing the same issue, can't seem to find where the problem is coming from...

matwithonet1 commented 3 years ago

Same problem here

morkeleb commented 3 years ago

Update: It's webpack that is renaming the classnames.

It looks like I could add keep_classnames to the minimizer function webpack is running. I'll post updates if I get it working

a-moss commented 3 years ago

@morkeleb FYI, the recommended solution here is to specify the tableName property in the @Table decorator. There's a section in the readme that discusses this, but it's kind of hidden near the bottom.

See: https://github.com/RobinBuschmann/sequelize-typescript#minification