test.group('Base Model | toObject',(group) => {
test('add preloaded hasOne relationship to toObject result', async ({ assert }) => {
class Profile extends BaseModel {
@column()
public username: string
@column()
public userId: number
}
class User extends BaseModel {
@column({ isPrimary: true })
public id: number
@column()
public username: string
@hasOne(() => Profile)
public profile: HasOne<typeof Profile>
}
const user = new User()
user.username = 'virk'
user.$setRelated('profile', null) // set the related null
user.toObject()
})
})
throw ERR:
Cannot read properties of null (reading 'toObject')
at anonymous src/Orm/BaseModel/index.ts:1930
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Package version
18.2.0
Node.js and npm version
Node.js 16.14.0 npm 8.3.1
Sample Code (to reproduce the issue)
modify test/orm/base-model.spec.ts to reproduce
throw ERR:
With commit: fix: set belongsTo and hasOne relationship property to null or model instance introduced in v14.0.0,
this.setRelated(parentModel, match || null)
makes it possible that relationship can be set to null explicitly.