question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get relation with trashed

See original GitHub issue

Feature Description

Get relation with trashed entities

The Problem

class User {
    @PrimaryGeneratedColumn({ type: 'bigint', unsigned: true })
    readonly id: string;

   @DeleteDateColumn()
    deleted_at: Date;

   @OneToMany(() => Review, (review: Review) => review.user)
    reviews: Promise<Review[]>;
}


class Review {
    @PrimaryGeneratedColumn({ type: 'bigint', unsigned: true })
    readonly id: string;

    @Index()
    @ManyToOne(() => User, (user: User) => user.reviews, { onDelete: 'CASCADE', nullable: false })
    @JoinColumn({ name: 'user_id', referencedColumnName: 'id' })
    user: Promise<User>;
}

When User is deleted than await review.user gives null. So when I need with trashed, I must use getRepository(User).createQueryBuilder()…etc.

The Solution

class Review {
    @PrimaryGeneratedColumn({ type: 'bigint', unsigned: true })
    readonly id: string;

    @Index()
    @ManyToOne(() => User, (user: User) => user.reviews, { onDelete: 'CASCADE', nullable: false, withDeleted: true })
    @JoinColumn({ name: 'user_id', referencedColumnName: 'id' })
    user: Promise<User>;
}

See withDeleted: true in ManyToOne

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don’t know how to start. I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
hastomcommented, Jul 22, 2021

@pleerock your solution does not look like it will query deleted photos. It will find user with specified id if it has non-deleted photos. But it got me thinking about an object notation for relations. What about:

usersRepository.find(id, {
  relations: {
    photos: { where: { id: 12 }, withDeleted: true } // and other find options for precise control of what will be loaded
  }
})

Maybe not full support of all find options but basics can be supported and even type-hinted

1reaction
hastomcommented, Jun 24, 2021

+1 Also it would be very nice to have an option to specify which trashed entities to include on per-query basis. Something like

usersRepository.find(id, { relations: ['photos', 'photos.comments:withDeleted'] })

or

usersRepository.find(id, { relations: ['photos'], relationsWithDeleted: ['photos.comments'] })
Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel eloquent with Trashed on relationship - Stack Overflow
You can use withTrashed method in different ways. To associate the call with your relationship you can do as follows:
Read more >
withTrashed on relationship (method) - Laracasts
withTrashed on relationship (method). According to the laravel 5.3 documentation ( https://laravel.com/docs/5.3/eloquent#querying-soft-deleted-models ).
Read more >
Laravel, “withTrashed()” linking a deleted relationship - Medium
If the user gets deleted, and on the User model we use the SoftDeletes trait, you can use withTrashed() method here. class Post...
Read more >
belongsTo() and withTrashed() - linking to deleted row
Let's say we have DB table products, which is linked to table categories with a field products.category_id = categories.id, Eloquent helps ...
Read more >
Render Relation List/Form with trashed datas - October Tricks
Go to your parent model, and change your relation like this : public $belongsTo = [ 'relationName' => [ \Author\Plugin\Models\YourRelationModel ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found