传递要创建的对象时出现sequelize-typescript错误

uwopmtnx  于 2022-12-14  发布在  TypeScript
关注(0)|答案(2)|浏览(158)

使用sequelize-typescript npm时尝试调用Street.create(obj)时出现错误:
类型'plainObject'的参数无法指派给类型'Optional〈Street,NullishPropertiesOf〉'的参数。类型'plainObject'遗漏类型'Omit〈Street,NullishPropertiesOf〉'的下列属性:续集、销毁、恢复、更新和39个其他
这是模型:

import { Table, Model, Column, PrimaryKey } from 'sequelize-typescript';

@Table({ timestamps: false, tableName: 'street' })
class Street extends Model<Street> {
    @PrimaryKey
    @Column
    street_id: string;

    @Column
    location_id: string;

    @Column
    location_symbol: string;

    @Column
    street_name: string;

    @Column
    street_synonym: string;

    @Column
    street_symbol: string;

    @Column
    updated: Date;
}

export default Street;

这是电话

Street.create({
                location_id: 'string',
                location_symbol: 'string',
                street_name: 'string',
                street_synonym: 'string',
                street_id: 'string',
                street_symbol: 'string',
                updated: new Date(),
            });
7cwmlq89

7cwmlq891#

您应该能够创建模型使用的接口
固定汇率:

import { Table, Model, Column, PrimaryKey } from 'sequelize-typescript';

interface IStreet {
    street_id: string;
    location_id: string;
    location_symbol: string;
    street_name: string;
    street_synonym: string;
    street_symbol: string;
    updated: Date;
}

@Table({ timestamps: false, tableName: 'street' })
class Street extends Model<IStreet> {
    @PrimaryKey
    @Column
    street_id: string;

    @Column
    location_id: string;

    @Column
    location_symbol: string;

    @Column
    street_name: string;

    @Column
    street_synonym: string;

    @Column
    street_symbol: string;

    @Column
    updated: Date;
}

Street.create({
    location_id: 'string',
    location_symbol: 'string',
    street_name: 'string',
    street_synonym: 'string',
    street_id: 'string',
    street_symbol: 'string',
    updated: new Date(),
});
lx0bsm1f

lx0bsm1f2#

我猜你用的是sequelize v6。
只要删除通用的<Street>,它就会工作。你不再需要它了。

import { Table, Model, Column, PrimaryKey } from 'sequelize-typescript';

@Table({ timestamps: false, tableName: 'street' })
class Street extends Model {
    @PrimaryKey
    @Column
    street_id: string;

    @Column
    location_id: string;

    @Column
    location_symbol: string;

    @Column
    street_name: string;

    @Column
    street_synonym: string;

    @Column
    street_symbol: string;

    @Column
    updated: Date;
}

export default Street;

相关问题