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

2

8

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.

Pyroligneous answered 30/6, 2021 at 14:47 Comment(0)
A
6

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.

Algophobia answered 19/7, 2021 at 11:33 Comment(0)
R
1

enter image description here Another thing is to pass sequelize datatype to the column function

Raylenerayless answered 3/1, 2023 at 10:19 Comment(1)
You seem to mean this as an answer, right? Would you like to explain a little on how to do that exactly, what effect it has and why that sovles the problem? Try for How to Answer. Maybe taket the tour.Antibiotic

© 2022 - 2024 — McMap. All rights reserved.