adonisjs / lucid

AdonisJS SQL ORM. Supports PostgreSQL, MySQL, MSSQL, Redshift, SQLite and many more
https://lucid.adonisjs.com/
MIT License
1.07k stars 191 forks source link

Fail in test('add preloaded hasOne relationship to toObject result') when setRelated with null #900

Closed haohanzhou123 closed 1 year ago

haohanzhou123 commented 1 year ago

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

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

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.

stale[bot] commented 1 year ago

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.