sequelize-typescript error when passing object to create
Asked Answered
E

3

5

While using the sequelize-typescript npm When trying to invoke the Street.create(obj) I get an error:

Argument of type 'plainObject' is not assignable to parameter of type 'Optional<Street, NullishPropertiesOf>'. Type 'plainObject' is missing the following properties from type 'Omit<Street, NullishPropertiesOf>': sequelize, destroy, restore, update, and 39 more

This is the model:

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;

this is the call

 Street.create({
                location_id: 'string',
                location_symbol: 'string',
                street_name: 'string',
                street_synonym: 'string',
                street_id: 'string',
                street_symbol: 'string',
                updated: new Date(),
            });
Embezzle answered 6/11, 2022 at 14:24 Comment(0)
P
8

I guess you are using sequelize v6.

Just remove generic <Street> and it will work. You don't need it anymore.

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;
Pegu answered 13/12, 2022 at 14:9 Comment(1)
This removes any typechecking on the call to create or bulkCreate. I'd recommend the other answer to avoid breaking the type system.Ostrom
M
2

You should be able to create an interface that the model uses

F.x.:

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(),
});
Monteiro answered 11/11, 2022 at 14:14 Comment(0)
S
0

The first answer removes type safety on your call to create, the second adds a duplicated type. This solves both issues

import { PrimaryKey } from 'sequelize-typescript'

@Table({ timestamps: false, tableName: 'street' })
class Street extends Model<
  InferAttributes<Street>,
  InferCreationAttributes<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
Shahaptian answered 20/9, 2024 at 8:23 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.