So I created a database schema with typeORM. So far this is one of the entities:

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;
  @Column()
  name: string;
  @Column()
  city: string;
  @Column()
  studio: string;
  @Column({ length: 1000 })
  description: string;
  @Column()
  address: string;
  @Column()
  profileImage: string;
  @Column()
  lastActiveDate: string;
  @Column()
  registrationDate: string;
  @Column()
  password: string;
  @OneToMany(() => User, (user) => user)
  followers: User[];
  @ManyToOne(() => User, (user) => user)
  following: User[];
  @OneToMany(() => Availability, (availability) => availability)
  timesAvailable: Availability[];
  @OneToMany(() => Offer, (offer) => offer)
  portfolio: Offer[];
  @OneToMany(() => Offer, (offer) => offer)
  offers: Offer[];
  @OneToMany(() => Offer, (offer) => offer)
  purchasedOffers: Offer[];
  @OneToMany(() => Chat, (chat) => chat)
  chats: Chat[];
  @OneToMany(() => Message, (message) => message)
  messages: Message[];
  @OneToMany(() => Meeting, (meeting) => meeting)
  meetings: Meeting[];
  @ManyToMany(() => Tag)
  @JoinTable()
  tags: Tag[];
}

The only part I’m having trouble with is this:

  @OneToMany(() => User, (user) => user)
  followers: User[];
  @ManyToOne(() => User, (user) => user)
  following: User[];

Essentially, I want one user to be able to follow may users, and many users to follow one. But doing this ^ causes this in the db:

r/webdev - TypeORM relations

Which is not what I want. All else works, aside from this.

And if I make it many to many, like this:

  @ManyToMany(() => User)
  @JoinTable()
  followers: User[];
  @ManyToMany(() => User)
  @JoinTable()
  following: User[];

I think it works. This is the effect:

r/webdev - TypeORM relations
r/webdev - TypeORM relations

And I mean, I think this works. But why does this have to be many to many to work? Shouldn’t one to many and many to one work?



Source link

Write A Comment