Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why joinColumns is undefined when trying to make a relation?

I'm getting TypeError: Cannot read property 'joinColumns' of undefined at <...>/src/persistence/SubjectOperationExecutor.ts:282:47 error when trying to create new object of entity with a relation. Why is this happening?

In one entity I have:

@Entity('users')
export class User extends WhateverEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string

  @ManyToMany(type => Account, account => account.users, {
    cascadeInsert: true,
    cascadeUpdate: true,
  })
  @JoinTable()
  accounts: Account[]
}

The second one:

@OneToOne(type => User, user => user.id)
owner: User

@ManyToMany(type => User, user => user.accounts)
users: User[]

And I save like this (where owner: User and users: User[]:

const account = new Account()
account.owner = owner
account.users = users || []

return this.accountRepository.save(account)

It doesn't work even if I comment out account.owner = owner or account.users = users || []. It saves account, but doesn't make the relation.


Additional info: if I console.log the account right before saving - it does have owner and users inside as instances of User class.


owner and users are User-entity objects that are already in database.

like image 521
Tomasz Gałkowski Avatar asked Dec 19 '17 11:12

Tomasz Gałkowski


1 Answers

You may forgot to put @JoinColumn() decorator on to your @OneToOne relation. It must be like there

@OneToOne(type => User, user => user.id)
@JoinColumn()
owner: User
like image 180
D.Zotov Avatar answered Nov 16 '22 02:11

D.Zotov