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

TypeScript HasOne association create requires foreign key that is automatically included #14365

Closed
1 of 5 tasks
garrettg123 opened this issue Apr 11, 2022 · 2 comments
Closed
1 of 5 tasks
Assignees
Labels
type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense.

Comments

@garrettg123
Copy link

Issue Creation Checklist

Bug Description

SSCCE

class Subscription extends Model<
  InferAttributes<Subscription>,
  InferCreationAttributes<Subscription>
> {
  declare id: CreationOptional<Scalars['ID']>
  declare createdAt: CreationOptional<Date>
  declare updatedAt: CreationOptional<Date>

  declare userId: Scalars['ID']

  declare iosTransactionReceipt: string
  declare isActive: boolean

  declare readonly user?: User

  declare static associations: {
    user: Association<Subscription, User>
  }
}
Subscription.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    createdAt: DataTypes.DATE,
    updatedAt: DataTypes.DATE,
    iosTransactionReceipt: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    isActive: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
    },
    userId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      unique: true,
    },
  },
  {
    sequelize: db,
  }
)

// Usage
user.createSubscription({
      isActive: true,
      iosTransactionReceipt,
    })

What do you expect to happen?

No TS error

What is actually happening?

Argument of type '{ isActive: true; iosTransactionReceipt: string; }' is not assignable to parameter of type 'Optional<InferCreationAttributes<Subscription, { omit: never; }>, NullishPropertiesOf<InferCreationAttributes<Subscription, { omit: never; }>>>'.
  Property 'userId' is missing in type '{ isActive: true; iosTransactionReceipt: string; }' but required in type 'Omit<InferCreationAttributes<Subscription, { omit: never; }>, NullishPropertiesOf<InferCreationAttributes<Subscription, { omit: never; }>>>'.ts(2345)
Output here

Additional context

Add any other context and details here.

Environment

  • Sequelize version: 6.18
  • Node.js version:
  • If TypeScript related: TypeScript version: 4.4
  • Database & Version:
  • Connector library & Version:

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.
@ephys ephys self-assigned this Apr 12, 2022
@ephys ephys added the type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense. label Apr 12, 2022
@ephys
Copy link
Member

ephys commented Apr 12, 2022

A new version of Sequelize 6 is being released with a backport of #14148, which should solve your issue

This is the relevant documentation to resolve your issue: https://sequelize.org/docs/v6/other-topics/typescript/#the-case-of-modelinit

TL;DR your class should look like this

class Subscription extends Model<
  InferAttributes<Subscription>,
  InferCreationAttributes<Subscription>
> {
  declare id: CreationOptional<Scalars['ID']>
  declare createdAt: CreationOptional<Date>
  declare updatedAt: CreationOptional<Date>

  declare userId: ForeignKey<Scalars['ID']> // I changed this line

  declare iosTransactionReceipt: string
  declare isActive: boolean

  declare readonly user?: NonAttribute<User> // and also this one

  declare static associations: {
    user: Association<Subscription, User>
  }
}

@ephys
Copy link
Member

ephys commented Apr 12, 2022

@ephys ephys closed this as completed Apr 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense.
Projects
None yet
Development

No branches or pull requests

2 participants