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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With