Skip to content
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

Failure when adding many-to-many association #6

Open
pcnova opened this issue May 6, 2022 · 0 comments · May be fixed by #7
Open

Failure when adding many-to-many association #6

pcnova opened this issue May 6, 2022 · 0 comments · May be fixed by #7
Labels
bug Something isn't working

Comments

@pcnova
Copy link

pcnova commented May 6, 2022

Description

We're encountering the following error when trying to add a new many-to-many association between two entities:

TypeError: Cannot read property 'constructor' of undefined

To be clear, we are not tracking changes on these entities (no @TrackChanges decorator), so getting an error from nestjs-changelog is quite unexpected. After some investigation, I figured out that the problem is a missing check for whether an entity is included in the event that notifies of a new insertion, here:

if (Reflect.hasMetadata("__track_changes", event.entity.constructor)) {

So this problem will be seen anytime there's something being inserted into the database which has not been marked with the @Entity decorator, as is the case for the join table created by TypeORM for a many-to-many relationship.

Workaround

One possibility, at least for this case, is to explicitly define an entity class for the join table (i.e., don't let TypeORM create it), which means you'll add the @Entity decorator, getting rid of the problem. However, this is not a simple change.

The easiest way I found around this is to use ChangeSubscriber.disableTracking to wrap the save() call. But of course, this may not be desirable if you are saving things that you want tracking on.

@pcnova pcnova added the bug Something isn't working label May 6, 2022
@pcnova pcnova linked a pull request Nov 6, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant