Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loadCount method change the model attributes #786

Closed
thetutlage opened this issue Feb 16, 2022 · 1 comment
Closed

loadCount method change the model attributes #786

thetutlage opened this issue Feb 16, 2022 · 1 comment
Assignees
Labels
Type: Bug The issue has indentified a bug

Comments

@thetutlage
Copy link
Member

Discussed in adonisjs/core#3441

Originally posted by farshadfahimi December 25, 2021
I have a simple model like below

import { DateTime } from 'luxon'
import {
  BaseModel,
  column,
  HasMany,
  hasMany,
  scope,
} from '@ioc:Adonis/Lucid/Orm'
import Product from 'App/Models/Product'

type PropertiesJson = {
  landing_categories: object
}

export default class Brand extends BaseModel {
  public serializeExtras = true

  @column({ isPrimary: true })
  public id: number

  @column()
  public name: string

  @column()
  public slug: string

  @column()
  public description: string | null

  @column.dateTime({ autoCreate: true, autoUpdate: true })
  public updatedAt: DateTime

  @hasMany(() => Product, {
    foreignKey: 'brandsId',
  })
  public products: HasMany<typeof Product>

}

and in the controller just fetch the one model from database

public async show({ params }: HttpContextContract) {
    const brand = await Brand.findOrFail(params.id)
    
    // await brand.loadCount('products')
    console.log(brand)
    return brand.serialize()
  }

the result of console in the controller is some thing like below

Brand {
  modelOptions: {
    connection: 'mysql',
    profiler: Profiler {
      appRoot: '/var/www/fafait/adonis-api',
      logger: [Logger],
      config: [Object]
    }
  },
  modelTrx: undefined,
  transactionListener: [Function: bound listener],
  fillInvoked: false,
  cachedGetters: {},
  '$columns': {},
  '$attributes': {
    id: 1,
    name: 'apple',
    slug: 'apple',
    updatedAt: DateTime {
      ts: 1618823508000,
      _zone: SystemZone {},
      loc: [Locale],
      invalid: null,
      weekData: null,
      c: [Object],
      o: 270,
      isLuxonDateTime: true
    }
  },
  '$original': {
    id: 1,
    name: 'apple',
    slug: 'apple',
    updatedAt: DateTime {
      ts: 1618823508000,
      _zone: SystemZone {},
      loc: [Locale],
      invalid: null,
      weekData: null,
      c: [Object],
      o: 270,
      isLuxonDateTime: true
    }
  },
  '$preloaded': {},
  '$sideloaded': {},
  '$isPersisted': true,
  '$isDeleted': false,
  '$isLocal': false,
  serializeExtras: true
}

but when uncomment await brand.loadCount('products') from controller the result o console change to

Brand {
  modelOptions: {
    connection: 'mysql',
    profiler: Profiler {
      appRoot: '/var/www/fafait/adonis-api',
      logger: [Logger],
      config: [Object]
    }
  },
  modelTrx: undefined,
  transactionListener: [Function: bound listener],
  fillInvoked: false,
  cachedGetters: {},
  '$columns': {},
  '$attributes': { id: 1 },
  '$original': { id: 1 },
  '$preloaded': {},
  '$extras': { products_count: 531 },
  '$sideloaded': {},
  '$isPersisted': true,
  '$isDeleted': false,
  '$isLocal': false,
  serializeExtras: true
}

as you can see the $attributes property data is changed and remove model properties and just save the id for use in eager loading
and if the controller code change to something like below and use withCount problem is solved

public async show({ params }: HttpContextContract) {
    const brand = await Brand.query().withCount('products').where({ id: params.id }).firstOrFail()

    return brand.serialize()
  }
```</div>
@thetutlage thetutlage transferred this issue from adonisjs/core Feb 16, 2022
@thetutlage thetutlage added the Type: Bug The issue has indentified a bug label Feb 16, 2022
@thetutlage thetutlage self-assigned this Feb 16, 2022
@thetutlage
Copy link
Member Author

It is a bug. Confirmed it on Discord

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug The issue has indentified a bug
Projects
None yet
Development

No branches or pull requests

1 participant