属性 的指定类型无法自动解析为 sequelize 数据类型。请手动定义数据类型

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

我在 sequelize-typescript 中定义 UserProduct 之间的一对多关系。

以下是模特:

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[]
}

我收到以下错误:

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

这是完整的堆栈跟踪:

*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)

如有任何意见,我们将不胜感激。

问题是 products 不是您的 table 的列。

这是一个由 Sequelize 创建的 属性 来查找您的关联模型。

因此,在您的情况下,您需要删除用户模型中 products 上方的 @Column。因为现在 Sequelize 正在尝试将其用作您 table 中存在的列,但这在您的数据库中不存在。