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

fix: resolve issue with find with relations returns soft-deleted entities #7296

Merged

Conversation

luiseariass
Copy link
Contributor

@luiseariass luiseariass commented Jan 19, 2021

Description of change

This pull request includes fix for soft delete behavior when including soft deletes relationships.
fix: resolve issue with find with relations returns soft-deleted entities #6265

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • npm run lint passes with this change
  • npm run test passes with this change
  • This pull request links relevant issues as Fixes #6265
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change
  • The new commits follow conventions explained in CONTRIBUTING.md

Copy link
Contributor

@nebkat nebkat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally squash the two commits into one now but otherwise looks good to me 🎉

@nebkat
Copy link
Contributor

nebkat commented Jan 19, 2021

Actually you should also add tests to verify the functionality, there should already be some examples testing non relation soft deleted items that you can mostly copy paste.

@luiseariass luiseariass reopened this Jan 19, 2021
@luiseariass luiseariass force-pushed the fix/soft-delete-in-join-tables branch 2 times, most recently from ff6ceb7 to fe0b221 Compare January 20, 2021 19:01
…ties

This new feature changes the behavior of typeorm to allow avoiding entities that have soft delete
close: typeorm#6265
@nebkat
Copy link
Contributor

nebkat commented Jan 22, 2021

Tests look reasonable. Strictly this should also be a "feat:" rather than "fix:" but not so important. One thing that will be an issue is that this is essentially a breaking change for current users so not sure how to work around that.

@caiomestres
Copy link

Tests look reasonable. Strictly this should also be a "feat:" rather than "fix:" but not so important. One thing that will be an issue is that this is essentially a breaking change for current users so not sure how to work around that.

A bool property for nested soft deletes wouldn't work @nebkat ?

something like
userRepository.find({ relations: ["profile", "photos", "videos"], nestedSoftDelete: true });

default would be false, that way maintaining backward compatibility

@nebkat
Copy link
Contributor

nebkat commented Jan 22, 2021

That works but it must be covered for both find and query builder, and it must also match the current naming (withDeleted I think).

@luiseariass
Copy link
Contributor Author

I propose we add a new flag withDeletedRelations: boolean to the @DeleteDateColumn decorator, with a default value of false. This way we don't affect current integrations.
The option withDeleted: true should overwrite withDeletedRelations: true behavior.

@nebkat
Copy link
Contributor

nebkat commented Jan 22, 2021

I don't think it should be on @DeleteDateColumn since that applies to the entity being deleted whether it is a relation or not. Whether deleted relations are included should be down to each query, and in future the default should switch to not including soft deleted relations.

@luiseariass
Copy link
Contributor Author

I was just looking for a way not to affect the current behavior, but I fervently believe that the behavior of the relationships should be the same as the main entity, we can work on updating the documentation of when the join is used and when it is used the soft delete

@nebkat
Copy link
Contributor

nebkat commented Jan 23, 2021

Personally I think there should just be a new major release with the breaking change but that doesn't seem to be planned so we have to work around it unfortunately.

@RAllner
Copy link

RAllner commented Jan 28, 2021

Hey guys. Are there any news on this PR? Looking forward to use it, since I could get rid of a bigger list of workarounds.

@caiomestres
Copy link

Hey guys. Are there any news on this PR? Looking forward to use it, since I could get rid of a bigger list of workarounds.

Waiting for @nebkat to deliberate about the best approach...

@brennedith
Copy link

@nebkat, following on @caiomestres' train of thought:

Find

const firstUser = await User.findOne({
  where: {
    id: 1,
  },
  withDeletedRelations: false,
});

QueryBuilder

const firstUser = await connection
  .getRepository(User)
  .createQueryBuilder('user')
  .withDeletedRelations(false)
  .where('user.id = :id', { id: 1 })
  .getOne();

Is this something that can be achieved? If so, can you suggest to us where to start?

Thanks in advance

@nebkat
Copy link
Contributor

nebkat commented Jan 29, 2021

Yes that looks like the ideal solution.

@pleerock
Copy link
Member

pleerock commented Feb 2, 2021

I don't think we need extra flag at least because it can raise questions like "how to do it with deleted relations but without deleted relations of relations".

Also I don't think we need a backward, because without this change current behaviour looks like a bug.

Thank you for contribution, I'm going to merge this one.

@pleerock pleerock merged commit d7cb338 into typeorm:master Feb 2, 2021
@luiseariass
Copy link
Contributor Author

when is the next release @pleerock?

@pleerock
Copy link
Member

pleerock commented Feb 9, 2021

Available in the latest 0.2.31 version.

@stovberpv
Copy link

Seems to work but withDeleted: true doesn't.

When i want to query deleted rows and use:

builder.withDeleted();

the query builds as follows:

SELECT *
  FROM plan
 INNER JOIN blank ON blank.id = plan.blank_id
                 AND (blank.deleted_at IS NULL)            <-- still here

@luiseariass
Copy link
Contributor Author

Seems to work but withDeleted: true doesn't.

When i want to query deleted rows and use:

builder.withDeleted();

the query builds as follows:

SELECT *
  FROM plan
 INNER JOIN blank ON blank.id = plan.blank_id
                 AND (blank.deleted_at IS NULL)            <-- still here

What query in typeom you use?

@stovberpv
Copy link

@luiseariass i have 2 entities with one-to-one relation

@Entity()
class Plan {
  @OneToOne(() => Blank, blank => blank.id)
  @JoinColumn({ name: 'blank_id' })
  blank: Blank;
}

@Entity()
class Blank {
  @OneToOne(() => Plan, plan => plan.blank)
  plan: Plan;

  @DeleteDateColumn()
  deleted_at: Date;
}

And query builder looks like this

const res = await this.manager
  .getRepository(Plan)
  .createQueryBuilder('plan')
  .innerJoinAndSelect(`plan.blank`, 'blank')
  .withDeleted()
  .getMany();

@stovberpv
Copy link

i figure it out, the withDeleted() needs to be added before the innerJoinAndSelect;

@jdnielss
Copy link

thank you for fix the bug ////

@br-dr3
Copy link

br-dr3 commented Mar 22, 2021

Any suggestion on how to bring relation deleted without flag? Many soft deletes just exist to continue showing some text of a deleted entity..

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

Successfully merging this pull request may close these issues.

None yet

9 participants