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

Polymorphic association

Open archansel opened this issue 4 years ago • 3 comments

Issue

Can we define polymorphic association?

Versions

  • "sequelize": "^6.6.5",
  • "sequelize-typescript": "^2.1.0",
  • "typescript": "^4.3.5"

Issue type

  • [ ] bug report
  • [x ] feature request

Actual behavior

I can't find any documentation related to polymorphism association

Expected behavior

The original sequelize support polymorphism association but I can't find a way using this package

archansel avatar Nov 05 '21 11:11 archansel

@archansel Hi, did you find any example? I also trying to find one.

onurguven avatar Nov 12 '21 06:11 onurguven

Nope I end up doing it manually to get the result I want

column name: contextId and contextType

@BelongsTo(() => Model1, 'contextId')
  model1: Model1;

  @BelongsTo(() => Model2, 'contextId')
  model2: Model2;

@Column(DataType.VIRTUAL)
  get title(): string {
    switch (this.contextType) {
      case Model1.name:
        return this.model1.title;
      case Model2.name:
        return this.model2.name;
      default:
        return null;
    }
  }

archansel avatar Dec 03 '21 03:12 archansel

Bump.

gabrieloczkowski avatar Jul 10 '23 08:07 gabrieloczkowski