Description
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.
- https://github.com/doug-martin/nestjs-query/blob/master/packages/query-sequelize/src/services/relation-query.service.ts#L170
- https://github.com/doug-martin/nestjs-query/tree/master/packages/query-sequelize/__tests__/__fixtures__
It could be the case that I am doing this incorrectly, if so please let me know!
Related code:
insert short code snippets here