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

Updating eager loaded relations fails #15

Closed
cloudratha opened this issue Jul 8, 2020 · 0 comments
Closed

Updating eager loaded relations fails #15

cloudratha opened this issue Jul 8, 2020 · 0 comments

Comments

@cloudratha
Copy link

It seems that there is a bug with Typeorm where updating a FK manually while the relation property is loaded causes multiple assignments to the join column. I.e. eager loaded relations: typeorm/typeorm#2651

I'm not sure if it's a safe solution but I found deleting the property from the instance before calling save does the trick.
I've only tested this on a OneToOne relation so not sure if it's the correct solution.

NB: setting the relation property to null doesn't seem to be enough, and needs to be off the instance.

Resource.ts

public async update(pk, params: any = {}): Promise<ParamsType>
    {
        params = this.prepareParamsBeforeSave(params);
        const instance = await this.model.findOne(pk);
        if(instance)
        {
            for(const p in params) {
                instance[p] = params[p];

                const property = this.property(p);
                if (property.type() === "reference") {
                    delete instance[property.column.relationMetadata.propertyName];
                }
            }
            await this.validate(instance);
            await instance.save();
            return instance;
        }

        throw new Error("Instance not found.");
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant