TypeORM one-to-one join in mongodb

Viewed 3274

I am new to TypeORM, so I am sorry in advance if this question sounds silly.

I have 2 tables User and Category. User has a column categoryId which is a foreign key to Category table.

Category table:

@Entity()
export class Category {

    @ObjectIdColumn()
    public _id: ObjectID;

    @PrimaryColumn()
    public id: string;

    public toString(): string {
        return `${this.id}`;
    }

}

User table

@Entity()
export class User {

    @ObjectIdColumn()
    public _id: ObjectID;

    @PrimaryColumn()
    public id: string;

    @Column()
    public categoryId: string;

    @OneToOne(type => Category)
    @JoinColumn()
    category: Category;

    public toString(): string {
        return `${this.id} ${this.categoryId}`;
    }

}

I want to fetch category details while querying for user. The query I am using is:

userRepository.findOne({ where, relations: ['category']});

Now the problem is when I use @JoinColumn(), it throws me an error.

MongoError: bad index key pattern {}: Index keys cannot be empty.

When I use @JoinTable(), I donot get expected result.

What are these @JoinColumn() and @JoinTable(). When should I use these ?

0 Answers
Related