# Nil entity returned when using dao select statement on Kong 3.3 Postgres

**URL:** <https://discuss.konghq.com/t/nil-entity-returned-when-using-dao-select-statement-on-kong-3-3-postgres/12265>\
**Category:** Questions\
**Created:** [January 9, 2024, 4:43pm UTC](https://discuss.konghq.com/t/nil-entity-returned-when-using-dao-select-statement-on-kong-3-3-postgres/12265 "2024-01-09T16:43:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Evelyn\_Chua](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/evelyn_chua/32/4303_2.png) [@Evelyn\_Chua](https://discuss.konghq.com/u/Evelyn_Chua)\
**Post date:** [January 9, 2024, 4:43pm UTC](https://discuss.konghq.com/t/nil-entity-returned-when-using-dao-select-statement-on-kong-3-3-postgres/12265/1 "2024-01-09T16:43:55Z")

</div>

After upgrading plugins from Kong 2.2.1 to Kong 3.3, under the following conditions:

1. I have a sample piece of lua code in a custom plugin that utilizes the kong cache function

```auto
local credential_cache_key = kong.db.oauth2_credentials:cache_key(client_id)
        client, err = kong.cache:get(credential_cache_key, nil,
            load_oauth2_credential_by_client_id,
            client_id)

```

where the callback function to retrieve the entity to be loaded into the cache is a dao select function

```auto
local function load_oauth2_credential_by_client_id(client_id)
    local credential, err = kong.db.oauth2_credentials:select_by_client_id(client_id)
    if err then
        return nil, err
    end

    return credential
end

```

1. Kong is deployed with the custom plugin in Kubernetes with more than one instance.

More often than not, on the initial start up, kong will hit the following error

```auto
[error] 1261#0: *4864420 [kong] init.lua:359 [partner-custom-oauth2] /opt/kong/plugins/partner-custom-oauth2/access.lua:1071: attempt to index local 'client' (a nil value)

```

indicating that the value in the cache/entity returned from the select statement is nil, even though a manual db query indicates that the entity exists but ws\_id is null.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/konghq/original/2X/6/6dac6a62116d00499103a40a30aecebf403777c4.png)

After a few kong restarts, it is noticed that the error goes away and when checking the oauth2\_credentials table the ws\_id is populated.

When turning on OpenTelemetry logs, it is noted that the dao select statement queries ws\_id as well, and having ws\_id as null could then be an issue

```auto
SELECT
  id,
  EXTRACT(EPOCH FROM created_at AT TIME ZONE 'UTC') AS created_at,
  expires_in,
  access_token,
  ws_id,
  FLOOR(EXTRACT(EPOCH FROM (ttl AT TIME ZONE 'UTC' - CURRENT_TIMESTAMP AT TIME ZONE 'UTC'))) AS ttl
.... (columns scrubbed to remove sensitive information)
FROM oauth2_tokens
WHERE
  access_token = 'jkCFYnyzN2gumQI9lbKgYTuHE63XNXDO'
  AND (ttl IS NULL OR ttl >= CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
  AND (ws_id = '6c431e2e-020c-4e4c-af58-a25f21b2d5de')
LIMIT 1;

```

Anyone has had a similar issue or know the cause? What causes the ws\_id to be null in the first place

---

<div class="post-metadata">

**Author:** ![Denis\_Souza](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/denis_souza/32/4884_2.png) [@Denis\_Souza](https://discuss.konghq.com/u/Denis_Souza)\
**Post date:** [April 17, 2025, 4:11pm UTC](https://discuss.konghq.com/t/nil-entity-returned-when-using-dao-select-statement-on-kong-3-3-postgres/12265/2 "2025-04-17T16:11:15Z")

</div>

Hi @Evelyn_Chua I’m facing exactly same issue going up to version 3.8.  
Did you figure it out what was hapenning?
