typescript - 如何使用 typescript 正确创建表格

标签 typescript sequelize.js sequelize-typescript

我正在尝试这样做:

interface UserI {
    id?: number | null,
    name: string,
    password: string
}


@Table(
    {
        tableName: 'user',
        timestamps: true
    }
)
class User extends Model implements UserI{

    @AutoIncrement
    @PrimaryKey
    @Column(INTEGER)
    id!: number

    @AllowNull(false)
    @NotEmpty
    @Column(STRING)
    name!: string

    @AllowNull(false)
    @NotEmpty
    @Column(STRING)
    password!: string

}

但不断报错:TypeError: Class constructor Model cannot be invoked without 'new';
我究竟做错了什么?

最佳答案

import { Model, Column, Table, AutoIncrement, PrimaryKey, NotEmpty, AllowNull } from "sequelize-typescript";

@Table(
    {
        tableName: 'user',
        timestamps: true
    }
)
export class User extends Model<User>{
    @AutoIncrement
    @PrimaryKey
    @Column
    id!: number

    @AllowNull(false)
    @NotEmpty
    @Column
    name!: string

    @AllowNull(false)
    @NotEmpty
    @Column
    password!: string
}
不需要接口(interface)。

关于typescript - 如何使用 typescript 正确创建表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62111327/

相关文章:

javascript - 对类数组对象进行排序 Angular

javascript - 如何创建具有多个可能相互冲突的要求的 .d.ts 文件

javascript - Sequelize Include On Through 关联

node.js - 如何从 Sequelize 模型中获取模式名称?

angular - ERROR 错误: Cannot find a differ supporting object 'No result' of type 'string' . NgFor只支持绑定(bind)到Iterables比如Arrays

javascript - Sequelize js upsert 控制主键是否为空

node.js - 使用 sequelize 初始化 postgres 数据库

node.js - 如何添加外键来 Sequelize 模型

database - Sequelize 减去 updatedAt 和 createdAt 转换成秒后

angular - TS2339 : Property 'assign' does not exist on type