Typeorm: ManyToOne relationship with composite key failing with "Referenced column eventId was not found in entity UserToEvent"

Viewed 1320

I am unable to create a new relation to my entity with composite primary key.

This is the entity:

@Entity({ name: 'user_events_event' })
export class UserToEvent {
    @PrimaryGeneratedColumn()
    id: number
    
    @ManyToOne(() => User, (user) => user.events, { primary: true })
    user: User
    
    @ManyToOne(() => Event, (event) => event.users, { primary: true })
    event: Event
    
    // what I am trying to add:
    @OneToMany(() => TrainingNoteEntity, (trainingNote) => trainingNote.participation)
    trainingNotes: Array<TrainingNoteEntity>
}

And the new entity:

@Entity({ name: 'training-note' })
export class TrainingNoteEntity {
    @PrimaryGeneratedColumn()
    id: number
    
    @ManyToOne(() => UserToEvent)
    @JoinColumn([
        { name: 'id', referencedColumnName: 'id' },
        { name: 'eventId', referencedColumnName: 'eventId' },
        { name: 'user', referencedColumnName: 'userId' },
    ])
    participation: UserToEvent
}

On the ManyToOne clause I would normally add the key only, but this generates an error stating that the entity can't be referenced fully So I added the @JoinColumn referencing all primary keys. The migrations are not generating due to an error

Referenced column eventId was not found in entity UserToEvent

I tried to change eventId -> event and userId -> user, but I get the same error.

0 Answers
Related