Skip to content

Calling $set with the relation name and null is not currently supported. #803

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

Open
1 of 2 tasks
doug-martin opened this issue Jun 26, 2020 · 1 comment
Open
1 of 2 tasks

Comments

@doug-martin
Copy link

doug-martin commented Jun 26, 2020

Versions

  • sequelize: 5.21.11
  • sequelize-typescript: 1.1.0
  • typescript 3.9.5

I'm submitting a ...

  • bug report
  • feature request

Actual behavior:

Setting a one-to-one relationship to null is not currently supported using $set or $remove.

I have the following models

@Table({ timestamps: false })
export class TestEntity extends Model<TestEntity> {
  @PrimaryKey
  @Column({ field: 'test_entity_pk' })
  testEntityPk!: string;

  @Column({ field: 'string_type' })
  stringType!: string;

  @Column({ field: 'bool_type' })
  boolType!: boolean;

  @Column({ field: 'number_type' })
  numberType!: number;

  @Column({ field: 'date_type' })
  dateType!: Date;

  @HasMany(() => TestRelation)
  testRelations?: TestRelation[];

  @BelongsToMany(() => TestRelation, () => TestEntityTestRelationEntity)
  manyTestRelations?: TestRelation[];

  @HasOne(() => TestRelation, 'oneTestEntityId')
  oneTestRelation?: TestRelation;
}
@Table({ timestamps: false })
export class TestRelation extends Model<TestRelation> {
  @PrimaryKey
  @Column
  testRelationPk!: string;

  @Column({ field: 'relation_name' })
  relationName!: string;

  @ForeignKey(() => TestEntity)
  @Column({ field: 'test_entity_id' })
  testEntityId?: string;

  @BelongsTo(() => TestEntity)
  testEntity?: TestEntity;

  @BelongsToMany(() => TestEntity, () => TestEntityTestRelationEntity)
  manyTestEntities?: TestEntity[];

  @BelongsTo(() => TestEntity, 'oneTestEntityId')
  oneTestEntity?: TestEntity;
}

The following code is from a framework called nestjs-query so it is pretty generic, but the following does not work

async removeRelation<Relation>(
    relationName: string,
    id: string | number,
    relationId: string | number,
  ): Promise<Entity> {
    const entity = await this.getById(id);
    const association = this.getAssociation(relationName);
    if (association.isSingleAssociation) {
      await entity.$set(relationName as keyof Entity, null);
    } else {
      await entity.$remove(relationName, relationId);
    }
    return entity;
  }

In the above removeRelation example the $remove does not work because it is a HasOne relation which makes sense and is similar to other ORMs, but I would expect to be able to call TestEntity.$set('oneTestRelation', null) in order to remove the relationship.

Expected behavior:

null should be an included in the union (Currently instances: R | R[] | string[] | string | number[] | number) type for the static $set method.

You can see the definition here https://github.com/RobinBuschmann/sequelize-typescript/blob/master/src/model/model/model.ts#L54

Of course I can currently get around this limitation by doing the following, but updating the union type would be preferable.

await entity.$set(relationName as keyof Entity, (null as unknown) as string);

Steps to reproduce:

You can use the above model definitions to reproduce or you can also view the framework to get a better idea of whats going on.

It could be the case that I am doing this incorrectly, if so please let me know!

Related code:

insert short code snippets here
@doug-martin
Copy link
Author

Looks like #774 will fix this.

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

1 participant