首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >指定类型的属性无法自动解析为序列化数据类型。请手工定义数据类型

指定类型的属性无法自动解析为序列化数据类型。请手工定义数据类型
EN

Stack Overflow用户
提问于 2021-06-30 22:47:46
回答 1查看 562关注 0票数 2

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

以下是模型:

Product.ts

代码语言:javascript
复制
@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

代码语言:javascript
复制
@Table
export class User extends Model {
    @Column({ primaryKey: true })
    id!: string

    @Column
    name!: string

    @Column
    email!: string

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

我得到以下错误:

代码语言:javascript
复制
Specified type of property 'products' cannot be automatically resolved to a sequelize data type. Please define the data type manually

下面是完整的堆栈跟踪:

代码语言:javascript
复制
*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)

任何意见都将非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-19 19:33:30

问题是products不是表中的一列。

这是Sequelize创建的一个属性,用于查找相关的模型。

因此,在您的示例中,您需要删除用户模型中products之上的@Column。因为就目前而言,Sequelize试图将其用作表中存在的列,但这在数据库中不存在。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68197033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档