Get relation with trashed
See original GitHub issueFeature 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:
- Created 2 years ago
- Reactions:3
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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:
Maybe not full support of all find options but basics can be supported and even type-hinted
+1 Also it would be very nice to have an option to specify which trashed entities to include on per-query basis. Something like
or