mysqltypescriptexpresssequelize.jssequelize-typescript

Specified type of property cannot be automatically resolved to a sequelize data type. Please define the data type manually


I'm defining one to many relationship between a User and a Product in sequelize-typescript.

Here are the Models:

Product.ts

@Table
export class Product extends Model {
    @Column({ primaryKey: true })
    id!: string

    @Column
    title!: string

    @Column({ type: DataType.DOUBLE })
    price!: number

    @Column
    imageUrl!: string

    @Column
    description?: string

    @ForeignKey(() => User)
    @Column
    userId?: string

    @BelongsTo(() => User)
    user?: User
}

User.ts

@Table
export class User extends Model {
    @Column({ primaryKey: true })
    id!: string

    @Column
    name!: string

    @Column
    email!: string

    @Column
    @HasMany(() => Product)
    products?: Product[]
}

I'm getting the following error:

Specified type of property 'products' cannot be automatically resolved to a sequelize data type. Please define the data type manually

Here's the complete stack trace:

*ProjectPath*/node_modules/sequelize-typescript/dist/model/shared/model-service.js:64
    throw new Error(`Specified type of property '${propertyName}'
          ^
Error: Specified type of property 'products'
            cannot be automatically resolved to a sequelize data type. Please
            define the data type manually
    at Object.getSequelizeTypeByDesignType (*ProjectPath*/node_modules/sequelize-typescript/dist/model/shared/model-service.js:64:11)
    at annotate (*ProjectPath*/node_modules/sequelize-typescript/dist/model/column/column.js:32:44)
    at Column (*ProjectPath*/node_modules/sequelize-typescript/dist/model/column/column.js:14:9)
    at DecorateProperty (*ProjectPath*/node_modules/reflect-metadata/Reflect.js:553:33)
    at Object.decorate (*ProjectPath*/node_modules/reflect-metadata/Reflect.js:123:24)
    at __decorate (*ProjectPath*/src/models/user.ts:4:92)
    at Object.<anonymous> (*ProjectPath*/src/models/user.ts:27:5)
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
    at Module.m._compile (*ProjectPath*/node_modules/ts-node/src/index.ts:1043:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1176:10)

Any input would be much appreciated.


Solution

  • The problem is that products is not a column of your table.

    It's a property created by Sequelize to find your associated models.

    So in your case you need to remove the @Column above products in your User model. Because for now Sequelize is trying to use it as a column existing in your table but this doesn't exist in your DB.