Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeORM sets relation to null when reloading NestJS application

When I reload my NestJS application the foreign key gets set to null.

User table:

@Entity()
export class User {
 @PrimaryGeneratedColumn()
 @OneToMany(() => Marker, (marker) => marker.user)
 @OneToMany(() => Vote, (vote) => vote.user)
 id: number;
 
 @Column
 name: string
}

Vote table:

@Entity()
export class Vote {
 @PrimaryGeneratedColumn()
 id: number
 
 @ManyToOne(() => User, (user) => user.id)
 user: User;
}

When I try the application everything works and in the vote table the user gets set and everything works. But when I restart the application the foreign key user from the table vote gets set to null. There are no error messages or warnings so I'm confused to what is going on.

like image 782
Mr Specht Avatar asked Oct 29 '25 05:10

Mr Specht


2 Answers

Delete the dist folder and re-run the server again.

like image 182
Solomon ABOYEJI Avatar answered Oct 30 '25 21:10

Solomon ABOYEJI


I agree with Solomon ABOYEJI -- Deleting the dist folder, rerunning the server seemed to fix this problem for me (in my case it was setting all values of a certain column to an empty string which caused my server to hang because that column has a unique constraint)

like image 27
happy2code Avatar answered Oct 30 '25 23:10

happy2code



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!