I have 2 entities that I want related to each other in 2 ways. I'm going to represent this as a Class and Students. I want all Students to belong to one Class but I want the Class to not only have many Students, but also a class president. The entities look like this right now:
@Entity()
export class Class extends BaseEntity {
@PrimaryGeneratedColumn("uuid")
id: string
@OneToOne(type => Student, { nullable: true })
@JoinColumn({ name: "classPresident", referencedColumnName: "id" })
classPresident?: Student
@OneToMany(type => Student, (student) => student.class, { nullable: true })
students?: Student[]
}
@Entity()
export class Student extends BaseEntity {
@PrimaryGeneratedColumn("uuid")
id: string
@ManyToOne(() => Class, (c) => c.students)
class?: Class
}
This isn't working how I would expect. I would expect the Class table to be created with column classPresident that is a key to a Student. I'm hoping to not have to do the keys myself, any criticism is welcome.
I can not reproduce your problem. What you expect is exactly what is happening when I use your entities.
Here is what typeorm logs:
Here is a screenshot of the structure created:
EDIT: My package.json:
My tsconfig.json:
To work with
extends BaseEntityI had to split each entity to its own file.