8000 hasManyThrough doesn't work properly with nullable localKey · Issue #1107 · adonisjs/lucid · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
hasManyThrough doesn't work properly with nullable localKey #1107
Open
@MohammedElattar

Description

@MohammedElattar

Package version

21.6.0

Describe the bug

When attempting to preload a hasManyThrough relation and the local key
(e.g. city_area_id) is null, Lucid throws an error instead of returning an empty relation or null.

Error Example

Exception: Cannot preload "city", value of "Vendor.city_area_id" is undefined

Expected Behavior

Lucid should handle null local keys gracefully and not throw errors when preloading relations. It should either:

  • Return null or [] based on the relation type.

Temporary Workaround

if (vendor.city_area_id) {
  await vendor.load('city')
} else {
  vendor.$setRelated('city', [])
}

Stack trace snippet

at ensureValue (...) {
  const value = collection[key];
  if (value === undefined || value === null) {
    missingCallback();
    return;
  }
}

Detailed trace

[05:17:27.396] ERROR (180839): Cannot preload "city", value of "Vendor.city_area_id" is undefined
    request_id: "m262nao1gmabmkx9otzulswv"
    x-request-id: "m262nao1gmabmkx9otzulswv"
    err: {
      "type": "Exception",
      "message": "Cannot preload \"city\", value of \"Vendor.city_area_id\" is undefined",
      "stack":
          Exception: Cannot preload "city", value of "Vendor.city_area_id" is undefined
              at file:///media/elattar/myData/html/wadjet/backend-adonis/node_modules/@adonisjs/lucid/build/src/utils/index.js:83:15
              at ensureValue (file:///media/elattar/myData/html/wadjet/backend-adonis/node_modules/@adonisjs/lucid/build/src/utils/index.js:31:9)
              at getValue (file:///media/elattar/myData/html/wadjet/backend-adonis/node_modules/@adonisjs/lucid/build/src/utils/index.js:82:12)
              at HasManyThroughQueryBuilder.addWhereConstraints (file:///media/elattar/myData/html/wadjet/backend-adonis/node_modules/@adonisjs/lucid/build/src/orm/relations/has_many_through/query_builder.js:66:23)
              at HasManyThroughQueryBuilder.applyConstraints (file:///media/elattar/myData/html/wadjet/backend-adonis/node_modules/@adonisjs/lucid/build/src/orm/relations/has_many_through/query_builder.js:139:14)
              at HasManyThroughQueryBuilder.exec (file:///media/elattar/myData/html/wadjet/backend-adonis/node_modules/@adonisjs/lucid/build/src/orm/relations/base/query_builder.js:146:14)
              at Preloader.processRelation (file:///media/elattar/myData/html/wadjet/backend-adonis/node_modules/@adonisjs/lucid/build/src/orm/preloader/index.js:41:57)
              at file:///media/elattar/myData/html/wadjet/backend-adonis/node_modules/@adonisjs/lucid/build/src/orm/preloader/index.js:137:25
              at Array.map (<anonymous>)
              at Preloader.processAllForOne (file:///media/elattar/myData/html/wadjet/backend-adonis/node_modules/@adonisjs/lucid/build/src/orm/preloader/index.js:136:54)
      "name": "Exception",
      "status": 500
    }

Reproduction repo

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0