export default class Folder extends Model<InferAttributes<Folder>, InferCreationAttributes<Folder>> {
@Attribute(DataTypes.INTEGER)
@PrimaryKey
@AutoIncrement
declare id: CreationOptional<number>;
@Attribute(DataTypes.STRING)
@NotNull
declare title: string;
@Attribute(DataTypes.INTEGER)
declare parentId: number;
declare parent?: NonAttribute<Folder>;
declare children?: NonAttribute<Folder[]>;
@HasMany(() => Folder, {
// as: 'children',
foreignKey: 'parentId',
inverse: {
as: 'parent'
},
})
For the HasMany association, if I don’t include as: 'children'
property, I get the following errors from sequelize :
SequelizeAssociationError: Defining HasMany association "parent" from _Folder to _Folder failed
Caused by: SequelizeAssociationError: Both options "as" and "inverse.as" must be defined for hasMany self-associations, and their value must be different.
However, if I include the as: 'children'
property, I get the following errors from typescript & sequelize :
Object literal may only specify known properties, and 'as' does not exist in type 'Omit<HasManyOptions<string, "id" | "title" | "parented">, "as">'.ts(2353)
Error: The "as" option is not allowed when using association decorators. The name of the decorated field is used as the association name.
How do I resolve this & get the self-association to work ?