-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Relation decorators: allow to pass string instead of typeFunction #4190
Comments
PR is submitted. As an example, the following pattern would now prevent circular dependency issues with IUser.ts export interface IUser {
id: number;
name: string;
} IPhoto.ts export interface IPhoto {
id: number;
url: string;
} User.ts @Entity()
export class User implements IUser {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@OneToMany('Photo', 'user')
photos: IPhoto[];
} Photo.ts @Entity()
export class Photo implements IPhoto {
@PrimaryGeneratedColumn()
id: number;
@Column()
url: string;
@ManyToOne('User', 'photos')
user: IUser;
} |
@haschu great work! |
@jordancue Thank you :) Hope 0.2.18 will be released soon. |
In my angular app, I have set "showCircularDependencies": false in the angular.json to simply ignore the circular dependency messages and it fixed my issues, it looks clean to me because there indeed are cycles in my typeORM models and this is fine. Am I missing something? What I prefer with this method over using strings is the maintainability aspect, I can still refactor the name of a class and have my IDE propagate the rename from the db down to the frontend. |
@Michael-Dawkins I guess you don't miss anything :)
Beware that circular dependencies can be pretty dangerous, as they lead to null/undefined references that may be hard to debug. That's why I find the warnings are overall helpful. TypeORM can deal with it, because it forwards the reference using a But if you by accident introduce another circular dependency, the compiler won't warn you, and debugging can be a hard time. |
Thank you for the explanation. It seemed weird to me at first why typeORM does not use |
You're welcome :) As you you're using Angular: have a look a forwardRef, it's the exact same use-case. |
Thanks @haschu , you save my life. Just change like this: From: From: |
@notHaiLuu thanks for sharing! I was getting an issue with Swagger from a NestJS project because that circular dependency |
@haschu, thanks good work, saves my day. :) |
I HOPE YOU YOU fulfill all your desires. I was going crazy. its first time i comment on a github. you are great @haschu |
You saved my day, thank you |
this caused issue on the frontend, see typeorm/typeorm#4190
this was causing issues on the frontend, see typeorm/typeorm#4190
@haschu 2021 almost 2022 finding this solution after a lot of searching thank you so much cowboy nice job! 🥇 |
Thanks for the PR fix , I am using typeorm with react-native unfortunately I still have this issue after implementing the fix, Works on a debug build but not when released I guess it has to do with the minified
|
@JohnTheBeloved try WatermelonDB instead, TypeORM is for Backend side (NodeJS), check my example for React Native here: https://github.com/proyecto26/MyApp |
@JohnTheBeloved Hello. I have the same problem when I define relations using string as suggested. However, I should point out that only in PRODUCTION mode. If I define a relation using string. Initializing the database fails with this error; If I do relationship definition normally. If I try to save a nested structure using the save method it fails with this error; Were you able to find any solution? |
A small update. I recreated the Entities using EntitySchema just to try it out. Details are here; https://typeorm.io/separating-entity-definition There are no problems now and everything is working fine. However, Typeorm does not support Subscribers and Listeners events for EntitySchema. I think it's the decorators that are causing the problem. I'm wondering how we can solve this. |
If anyone is still struggling with these issues, check out this link. #4714 I am using Typeorm with React native. And the solution in the link makes your application run successfully in production mode. |
good |
Issue type:
[ ] question
[ ] bug report
[x] feature request
[ ] documentation issue
To define table relations using decorators, this is the current way:
Photo.ts
User.ts
This works fine on Node.js, but can cause circular dependency issues on the frontend side (e.g. when using Angular CLI). See: #2059 or #1290
We could overcome those issues, if the following would be possible:
Photo.ts
User.ts
This should be fairly easy to implement, as it turns out that TypeORM already supports
string
instead of types (e.g. when usingEntitySchema
) - the only thing to do is to adjust thetypeFunction
type of the decorator functions.@pleerock If that feature would be ok I could post a PR :)
The text was updated successfully, but these errors were encountered: