Skip to content

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

Open
@doug-martin

Description

@doug-martin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions