For the past three days, I’ve been trying to integrate Sequelize M:N (many-to-many) associations with TypeScript.
The issue arises when I follow the official Sequelize documentation, which suggests defining associations alongside the model definitions. This works initially, but when I attempt to implement the association through a join (proxy) table, I encounter the following error: .belongsTo called with something that's not a subclass of Sequelize.Model
. I suspect this is due to a circular dependency. If this isn’t the correct approach, why does it appear in the official docs? (Rhetorical question.)
Moving on, I decided to add an associate
method to each individual model and call it after initializing all models. However, this led to some awkward workarounds, such as needing to write (ImportConfig as any).associate =
for every model. Now, I’m facing a new challenge: dynamically importing models from a directory since the model files have a .ts
extension.
Does anyone know a better way to organize this? I’d prefer not to manually add static imports to the initialization script every time I add a new model.