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

Errors when defining @ForeignKey and @BelongsTo decorator #752

Closed
Johnathon332 opened this issue Mar 18, 2020 · 2 comments
Closed

Errors when defining @ForeignKey and @BelongsTo decorator #752

Johnathon332 opened this issue Mar 18, 2020 · 2 comments

Comments

@Johnathon332
Copy link

Versions

  • sequelize: 5.21.5
  • sequelize-typescript: 1.1.0
  • typescript: 3.8.3

I'm submitting a ...

[x ] bug report
[ ] feature request

Actual behavior:

There are actually two errors that I am facing one of which is with the @foreignkey decorator and the other being the @BelongsTo decorator.

  • Foreign key error being

Property 'isInitialized' is missing in type 'typeof Product' but required in type 'typeof Model'.

  • BelongsTo error being

No overload matches this call.
Overload 1 of 2, '(associatedClassGetter: ModelClassGetter, foreignKey?: string | undefined): Function', gave the following error.
Type 'typeof Product' is not assignable to type 'typeof Model'.
Overload 2 of 2, '(associatedClassGetter: ModelClassGetter, options?: BelongsToOptions | undefined): Function', gave the following error.
Type 'typeof Product' is not assignable to type 'typeof Model'

Expected behavior:
I followed the documentation of how to define these decorators, this should work

Steps to reproduce:

Here is the application I am working on https://github.com/Johnathon332/FirstNodeApp

Related code:

import { Model } from 'sequelize/types';
import {
  Table,
  Column,
  CreatedAt,
  UpdatedAt,
  DataType,
  IsDate,
  NotEmpty,
  PrimaryKey,
  AutoIncrement,
  AllowNull,
} from 'sequelize-typescript';

@Table
export class Product extends Model<Product> {
  @PrimaryKey
  @AutoIncrement
  public id!: number;

  @Column(DataType.STRING)
  @AllowNull(false)
  @NotEmpty
  public name!: string;

  @Column(DataType.DECIMAL)
  @AllowNull(false)
  @NotEmpty
  public price!: number;

  @CreatedAt
  public readonly createdAt!: Date;

  @UpdatedAt
  public readonly updatedAt!: Date;
}
import { Model } from 'sequelize/types';
import {
  Table,
  Column,
  NotNull,
  DataType,
  PrimaryKey,
  AutoIncrement,
  ForeignKey,
  CreatedAt,
  UpdatedAt,
  BelongsTo,
} from 'sequelize-typescript';
import { Product } from './Product';

@Table
export class Order extends Model<Order> {
  @PrimaryKey
  @AutoIncrement
  @Column
  public id!: number;

  @ForeignKey(() => Product)
  @Column
  public productId!: number;

  @BelongsTo(() => Product)
  public product!: Product;

  @Column(DataType.NUMBER)
  public quantity!: number;

  @CreatedAt
  public createdAt!: Date;

  @UpdatedAt
  public updatedAt!: Date;
}
@RobinBuschmann
Copy link
Member

Hey @Johnathon332, you've imported the wrong Model reference:

- import { Model } from 'sequelize/types';
import {
+ Model,
  Table,
  Column,
  NotNull,
  DataType,
  PrimaryKey,
  AutoIncrement,
  ForeignKey,
  CreatedAt,
  UpdatedAt,
  BelongsTo,
} from 'sequelize-typescript';
import { Product } from './Product';

@Table
export class Order extends Model<Order> {

@RobinBuschmann
Copy link
Member

Closing this due to inactivity

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

2 participants