sequelize-typescript icon indicating copy to clipboard operation
sequelize-typescript copied to clipboard

getting error missing following properties on create code

Open yozawiratama opened this issue 3 years ago • 7 comments

Issue

error create code

Argument of type '{ name: string; email: string; phone: number; }' is not assignable to parameter of type 'User'. Type '{ name: string; email: string; phone: number; }' is missing the following properties from type 'User': $add, $set, $get, $count, and 32 more.

Versions

  • sequelize: ^6.5.0
  • sequelize-typescript: 2.1.0
  • typescript: newest
await User.create({ name: 'xxx', email: '[email protected]', phone: 0 });

users.ts

import { Table, Column, Model, HasMany, Default, DataType, CreatedAt, UpdatedAt, PrimaryKey, IsUUID, AllowNull } from 'sequelize-typescript'
import { EnumDataType } from 'sequelize/types';

type Roles = 'ADMIN' | 'STAFF' | 'CUSTOMER';
type Statuses = 'ACTIVE' | 'INACTIVE';

@Table({
    timestamps: true,
})
export class User extends Model<User> {

    @IsUUID(4)
    @PrimaryKey
    @Default(DataType.UUIDV4)
    @Column(DataType.UUID)
    id?: string;

    @Column(DataType.UUID)
    brokerId?: string;

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

    @AllowNull(false)
    @Column
    email!: string;

    @AllowNull(false)
    @Column
    phone!: number;

    @AllowNull(false)
    @Default('CUSTOMER')
    @Column(DataType.ENUM('ADMIN', 'STAFF', 'CUSTOMER'))
    role?: Roles;

    @AllowNull(false)
    @Default('ACTIVE')
    @Column(DataType.ENUM('ACTIVE', 'INACTIVE'))
    status?: Statuses;

    @CreatedAt
    @Column
    createdAt?: Date;

    @UpdatedAt
    @Column
    updatedAt?: Date;

}

yozawiratama avatar Mar 09 '21 03:03 yozawiratama

but when i convert from

export class User extends Model<User> {

to

export class User extends Model {

it works

but that make me create manual interface,

can somebody helpe solve this problem?

yozawiratama avatar Mar 09 '21 03:03 yozawiratama

@yozawiratama This is not the best solution but should work:

class AttributedModel<T> extends Model<Omit<T, keyof Model>> {}

@Table
export default class User extends AttributedModel<User> {
  @PrimaryKey
  @Column
  id: number;

  @Column
  name: string;
}

KapitanOczywisty avatar Mar 22 '21 07:03 KapitanOczywisty

I met the same problem, do you have a better plan?

youmengme avatar Mar 26 '21 16:03 youmengme

Got the same problem, solved it with this: export class Clinic extends Model<Partial<Clinic>> {

xpolb01 avatar Mar 31 '21 18:03 xpolb01

Got the same problem, solved it with this: export class Clinic extends Model<Partial<Clinic>> {

This is one of solution for the problem, really good

but I got error when I use associations, using includes

@HasMany(() => Records)
  records?: Records[];
new Promise(async (resolve, reject) => {
      try {
        const result = await Users.findAll({
          include: [Records], // <- connect has many records in table records
          where: { Role: Role },
          order: [["CreatedAt", "DESC"]],
        });
        resolve(result);
      } catch (error) {
        reject(error);
      }
    });

Type 'Partial<Records>' is not assignable to type 'TModelAttributes'. 'Partial<Records>' is assignable to the constraint of type 'TModelAttributes', but 'TModelAttributes' could be instantiated with a different subtype of constraint '{}'.

yozawiratama avatar Apr 05 '21 07:04 yozawiratama

I am also having the same problem. Is there no optimal solution to this problem?

zSakuraEvilz avatar Apr 27 '22 05:04 zSakuraEvilz

Hello!

I can confirm that using Partial in the model definition removes the error message.

Another solution is to disable null checks in .tsconfig.json with "strictNullChecks": false.

Better solutions are welcome, though.

Cheers, Cocco

coccoinomane avatar May 21 '22 02:05 coccoinomane