錯誤:不明或無法存取的欄位

在處理 LookML 檔案並滿意更新後,下一步就是執行 LookML 驗證工具,以便執行完整的模型驗證,並部署 LookML 變更。

有時您可能會看到類似以下的錯誤訊息:

 Unknown or inaccessible field "user_order_facts.lifetime_orders" referenced in "users.lifetime_orders". Check for typos and missing joins.

在這個範例中,錯誤是指 users 檢視畫面中的 lifetime_orders 欄位。這項錯誤表示 users.lifetime_orders 無法存取其參照的 user_order_facts.lifetime_orders 欄位。

為什麼會發生這項錯誤?

導致這項錯誤發生的原因包括:

  1. 您參照的欄位不存在
  2. 您參照的欄位是整個維度群組,例如,您參照的維度群組沒有附加 timeframe
  3. 部分探索功能無法存取該欄位,因為缺少了彙整

方法 1:找不到欄位

如果 LookML 欄位中參照的欄位 user_order_facts.lifetime_orders 本身並未存在,就會收到 unknown or inaccessible field 錯誤。

如要解決這項錯誤,請將觸發錯誤的欄位 (在本例中為 user_order_facts.lifetime_orders) 新增至包含問題欄位的檢視畫面。在這種情況下,您可以確認欄位是否已在 user_order_facts 檢視畫面中定義;如果不存在,您可以新增該欄位。

選項 2:欄位參照整個維度群組

維度群組代表一組維度。type: time 維度群組代表在 timeframe 參數中定義的一組時間範圍維度。在 LookML 中參照維度群組時,您必須將適當的維度 (在本例中為 timeframe) 附加至維度群組名稱。

舉例來說,請考量下列維度群組:

  dimension_group: created {
    type: time
    timeframes: [date, week, month]
    sql: ${TABLE}.created_at ;;
  }

如果您想在其他 LookML 欄位中參照 created 維度群組,就必須參照群組中的特定時間範圍維度,例如下列任一項:

  • date${created_date}
  • week${created_week}
  • month${created_month}

如果您嘗試只使用維度群組的名稱 (${created}),Looker 將無法得知您指的是哪個時間範圍,並產生錯誤。

選項 3:缺少彙整

以下是 users.lifetime_orders 的 LookML 定義:

  dimension: lifetime_orders {
    type: number
    sql: ${user_order_facts.lifetime_orders};;
  }
請注意,替換運算子 ${}用於參照 LookML 欄位 user_order_facts.lifetime_orders

users 檢視畫面中的 lifetime_orders 維度會參照 user_order_facts 檢視畫面中的 lifetime_orders 欄位。在這種情況下,系統會觸發錯誤,因為模型檔案中存在 users 檢視畫面已與探索項目結合的執行個體, user_order_facts 並未一併加入。

如要查看哪些 Explore 導致問題,您可以展開錯誤訊息中醒目顯示的項目:

展開的錯誤訊息顯示兩個原因:users:79 (ecommerce:order_items) 和 users:79 (ecommerce:orders) 的檢視畫面、檢視畫面程式碼行和探索。

這些事件顯示 ecommerce 模型中的 order_itemsorders 探索會導致錯誤。這些探索包含許多彙整,並在模型檔案中定義如下:

  explore: orders {
    join: users { # users joined without user_order_facts
      relationship: many_to_one
      sql_on: ${orders.user_id} = ${users.id}
    }
  }

  explore: order_items {
    join: inventory_items {
      relationship: many_to_one
      sql_on: ${order_items.inventory_item_id} = ${inventory_items.id}
    }
    join: orders {
      relationship: many_to_one
      sql_on: ${order_items.order_id} = ${orders.id}
    }
    join: users { # users joined without user_order_facts
      relationship: many_to_one
      sql_on: ${orders.user_id} = ${users.id}
    }
  }

在上述兩個探索中,users 檢視畫面user_order_facts 檢視畫面結合,因此兩個探索都無法存取 user_order_facts.lifetime_orders 欄位。如果您嘗試在任一探索中查詢參照 user_order_facts.lifetime_ordersusers.lifetime_orders 欄位,就會觸發錯誤。

LookML 驗證工具會警告您,使用者在查詢 users_order_facts.lifetime_orders 時會收到錯誤。users.lifetime_orders 欄位不會在 user_order_facts 也已彙整的探索中觸發錯誤。

例如,請考量 users Explore:

  explore: users {
    join: user_order_facts {
      sql_on: ${users.id} = ${user_order_facts.users_id}
    }
  }

此處 user_order_facts 已彙整,因此查詢 users.lifetime_orders 不會觸發錯誤。

如何修正缺少彙整作業所造成的錯誤?

如果錯誤是因為缺少彙整作業而發生,您可以透過幾種方式修正這項錯誤:

  1. 所有案件中加入缺少的檢視畫面。針對本頁面中使用的範例,請確認 user_order_facts 檢視畫面與 users 檢視畫面在「探索」中彙整的任何位置都已彙整。
  2. 如果不想彙整缺少的檢視畫面,請排除導致錯誤的欄位。

加入缺少的檢視畫面

在上述範例中,只要將 user_order_facts 與所有已彙整 users 的探索報表彙整,即可解決錯誤。這樣一來,當查詢中使用 users.lifetime_orders 時,探索功能就能存取 user_order_facts.lifetime_orders

您可以使用 IDE 中的中繼資料面板,查看所有使用 users 檢視畫面的探索。

以下範例會彙整缺少的檢視畫面:

  explore: order_items {
    join: inventory_items {
      relationship: many_to_one
      sql_on: ${inventory_items.id} = ${order_items.inventory_item_id}
    }
    join: orders {
      relationship: many_to_one
      sql_on: ${order_items.order_id} = ${orders.id}
    }
    join: users {
      relationship: many_to_one
      sql_on: ${orders.user_id} = ${users.id}
    }
    join: user_order_facts { # join user_order_facts through users
      relationship: many_to_one
      sql_on: ${users.id} = ${user_order_facts.users_id}
    }
  }

現在,如果您重新執行 LookML 驗證工具,就不會看到這項錯誤。

從探索中排除導致錯誤的欄位

您可能不想將 user_order_facts 檢視畫面與已彙整 users 的所有探索資料彙整。舉例來說,您可能不希望使用者從 orders 探索中的 user_order_facts 檢視畫面存取欄位,但希望使用者能從 users 檢視畫面存取欄位而不會發生錯誤。您可以使用 fields 參數,從 orders Explore 中排除導致錯誤的欄位 (users.lifetime_orders),

探索的 fields 參數可讓您納入或排除探索中的特定欄位。在這種情況下,您可以從 orders Explore 中排除 users.lifetime_orders 欄位,如下所示:

  explore: orders {
    fields: [-users.lifetime_orders] # exclude users.lifetime_orders
    join: users {
      relationship: many_to_one
      sql_on: ${orders.user_id} = ${users.id}
    }
  }