Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model.build(), Model.create(), modelInstance.set() do not accept an object with an association. #1379

Open
1 of 2 tasks
maqduni opened this issue Jul 30, 2022 · 5 comments
Open
1 of 2 tasks

Comments

@maqduni
Copy link

maqduni commented Jul 30, 2022

Issue

Following methods do not accept an object with an association,

  • Model.build()
  • Model.create()
  • modelInstance.set()

Versions

  • sequelize: 6.21.2
  • sequelize-typescript: 2.1.3
  • typescript: 4.7.4

Issue type

  • bug report
  • feature request

Actual behavior

Getting the following compiler error,

TS2345: Argument of type '{ email: string; role: Role; memberProfile: { interests: string[]; }; firstName: string; lastName: string; }' is not assignable to parameter of type 'Optional<User, NullishPropertiesOf>'.   Type '{ email: string; role: Role; memberProfile: { cohort: string; }; firstName: string; lastName: string; }' is not assignable to type 'Partial<Pick<User, NullishPropertiesOf>>'.     Types of property 'memberProfile' are incompatible.       Type '{ interests: string[]; }' is missing the following properties from type 'MemberProfile': userId, $add, and 35 more.

Expected behavior

Should be able to pass an object with an association. Would like the initial data object type to be similar to RecursivePartial<>, i.e. RecursiveNullishPropertiesOf<>

Steps to reproduce

Please use the code below to reproduce.

Related code

export class User extends Model<User> {
  @Column
  public email: string;

  @Column
  public role: Role;

  @Column
  public firstName?: string;

  @Column
  public lastName?: string;

  @HasOne(() => MemberProfile, 'userId')
  public memberProfile?: MemberProfile;
}

export class MemberProfile extends Model<MemberProfile> {
  @Column
  userId: number;

  @Column
  interests?: string[];
}

const seedUser = {
  email: 'christiano@ronaldo.com',
  role: Role.member,
  memberProfile: {
    interests: ['soccer', 'cooking'],
  },
  firstName: 'Christiano',
  lastName: 'Ronaldo',
};

const newUser = User.build(seedUser);
@cHenrique0
Copy link

Try create a interface for model attributes. E.g:

interface UserAttributes {
  email: string;
  role: Role;
  firstName?: string;
  lastName?: string;
}

class User extends Model<UserAttributes> {
  /* code */
}

It works for me.

@maqduni
Copy link
Author

maqduni commented Aug 9, 2022

Try create a interface for model attributes. E.g:

interface UserAttributes {
  email: string;
  role: Role;
  firstName?: string;
  lastName?: string;
}

class User extends Model<UserAttributes> {
  /* code */
}

It works for me.

The problem is when you have an association in your interface. Simple data types work in my set up as well. And my model uses itself as an interface export class User extends Model<User> {

@broxiang
Copy link

you probably want to declare models like this: https://github.com/RobinBuschmann/sequelize-typescript#model-definition

your example code is usingexport class User extends Model<User>, which is definition for sequelize v5

@broxiang
Copy link

you can change your code to export class User extends Model {}

@oktapodia
Copy link

you can change your code to export class User extends Model {}

Just to optimize the search, Fixes for errors TS2740 and TS2322

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants