# Db pull and lookup refs issue

**URL:** <https://forum.datomic.com/t/db-pull-and-lookup-refs-issue/2130>\
**Category:** Troubleshooting\
**Created:** [September 14, 2022, 4:05pm UTC](https://forum.datomic.com/t/db-pull-and-lookup-refs-issue/2130 "2022-09-14T16:05:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikolayandr](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/nikolayandr/32/375_2.png) [@nikolayandr](https://forum.datomic.com/u/nikolayandr)\
**Post date:** [September 14, 2022, 4:05pm UTC](https://forum.datomic.com/t/db-pull-and-lookup-refs-issue/2130/1 "2022-09-14T16:05:10Z")

</div>

According to [the docs](https://docs.datomic.com/cloud/query/query-pull.html) lookup refs can be used in pull function

No expected result is returned if use [pull](https://docs.datomic.com/client-api/datomic.client.api.html#var-pull) with lookup refs, steps to reproduce:

```auto
(require '[datomic.client.api :as d])
(def cfg {:server-type :peer-server
:access-key "datomic"
:secret "datomic"
:endpoint "localhost:8998"
:validate-hostnames false})
(def client (d/client cfg))
(def conn (d/connect client {:db-name "database"}))

(def user-schema [
                   {
                       :db/ident :User/name
                       :db/valueType :db.type/string
                       :db/cardinality :db.cardinality/one
                       :db/unique :db.unique/value
                   }
                   ])
(d/transact conn {:tx-data user-schema})

(def user-1-name "User")
(def user-data [
  {
    :User/name user-1-name
  }
])
(d/transact conn {:tx-data user-data})

(def db (d/db conn))
(d/pull db '[*]' [:User/name user-1-name] )

```

The result is:

> #:db{:id nil}

The same actions executed using peer API work well

Env:  
Datomic 1.0.6202

---

<div class="post-metadata">

**Author:** ![jaret](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/jaret/32/45_2.png) [@jaret](https://forum.datomic.com/u/jaret)\
**Post date:** [September 16, 2022, 2:42pm UTC](https://forum.datomic.com/t/db-pull-and-lookup-refs-issue/2130/2 "2022-09-16T14:42:44Z")

</div>

Hey @nikolayandr I reproduced this behavior and it looks like a bug to me. I am logging a story for us to evaluate a fix.

---

<div class="post-metadata">

**Author:** ![jaret](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/jaret/32/45_2.png) [@jaret](https://forum.datomic.com/u/jaret)\
**Post date:** [September 16, 2022, 3:31pm UTC](https://forum.datomic.com/t/db-pull-and-lookup-refs-issue/2130/3 "2022-09-16T15:31:44Z")

</div>

Actually, @nikolayandr I spent some more time looking at this and I don’t think it’s the bug I thought it was. You have a typo in your example adding a `'` _after_ the `[*]`. I created a gist here testing on various Datomic versions and confirmed that it works to pull without that extra `'`. I think we should probably behave a little better and throw a warning or something because I don’t think that syntax is actually valid. So I am going to follow up on that.

```auto
user=> (d/pull db '[*]' [:User/name user-1-name] )
#:db{:id nil}
user=> (d/pull db '[*] [:User/name user-1-name] )
{:db/id 17592186045418, :User/name "User"}
user=>

```

Gist work:

> <https://gist.github.com/Jaretbinford/4592318cdabb41487e2c9365bf4eb8b8>

---

<div class="post-metadata">

**Author:** ![nikolayandr](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/nikolayandr/32/375_2.png) [@nikolayandr](https://forum.datomic.com/u/nikolayandr)\
**Post date:** [September 20, 2022, 8:15am UTC](https://forum.datomic.com/t/db-pull-and-lookup-refs-issue/2130/4 "2022-09-20T08:15:42Z")

</div>

Thank you for helping, works for me

---

<div class="post-metadata">

**Author:** ![nikolayandr](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/nikolayandr/32/375_2.png) [@nikolayandr](https://forum.datomic.com/u/nikolayandr)\
**Post date:** [September 26, 2022, 8:07am UTC](https://forum.datomic.com/t/db-pull-and-lookup-refs-issue/2130/5 "2022-09-26T08:07:41Z")

</div>

> You have a typo in your example adding a `'` _after_ the `[*]`

But this ‘typo’ works when entity id is used to identify an entity 🙂  
I mean if instead of `[:User/name user-1-name]` to use the id of entity ` (d/pull db '[*]' 123324)` then the expected result is returned

---

<div class="post-metadata">

**Author:** ![nikolayandr](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/nikolayandr/32/375_2.png) [@nikolayandr](https://forum.datomic.com/u/nikolayandr)\
**Post date:** [September 27, 2022, 1:24pm UTC](https://forum.datomic.com/t/db-pull-and-lookup-refs-issue/2130/6 "2022-09-27T13:24:44Z")

</div>

@jaret Could you please tell me, if the function [db](https://docs.datomic.com/client-api/datomic.client.api.html#var-db) always calls the peer server to get the last available state of DB? Depending on your answer I still might have an issue with pulling data

---

<div class="post-metadata">

**Author:** ![nikolayandr](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/nikolayandr/32/375_2.png) [@nikolayandr](https://forum.datomic.com/u/nikolayandr)\
**Post date:** [September 27, 2022, 2:30pm UTC](https://forum.datomic.com/t/db-pull-and-lookup-refs-issue/2130/7 "2022-09-27T14:30:17Z")

</div>

Maybe my question should sound like the next:  
How to get DB available on the peer server? Should [connection](https://docs.datomic.com/client-api/datomic.client.api.html#var-connect) always be created or is it enough to call [db](https://docs.datomic.com/client-api/datomic.client.api.html#var-db) function?

If I call [db](https://docs.datomic.com/client-api/datomic.client.api.html#var-db) function, can I be sure the last state of DB available on the peer server is returned by the function?

---

<div class="post-metadata">

**Author:** ![nikolayandr](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/nikolayandr/32/375_2.png) [@nikolayandr](https://forum.datomic.com/u/nikolayandr)\
**Post date:** [September 27, 2022, 3:02pm UTC](https://forum.datomic.com/t/db-pull-and-lookup-refs-issue/2130/8 "2022-09-27T15:02:17Z")

</div>

I noticed the next behavior: calling of [db](https://docs.datomic.com/client-api/datomic.client.api.html#var-db) function in forever cycle makes network communication with peer server only each ~2secs and I see **basis t** of connection is updated(in case newer DB is available on peer server) when network communication has happened

---

<div class="post-metadata">

**Author:** ![nikolayandr](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/nikolayandr/32/375_2.png) [@nikolayandr](https://forum.datomic.com/u/nikolayandr)\
**Post date:** [September 27, 2022, 3:36pm UTC](https://forum.datomic.com/t/db-pull-and-lookup-refs-issue/2130/9 "2022-09-27T15:36:51Z")

</div>

Another interesting behavior: if to execute some function that requires network communication with peer server(e.g. pull) then **basis t** of connection is updated(in case newer DB is available on peer server)

---

<div class="post-metadata">

**Author:** ![nikolayandr](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/nikolayandr/32/375_2.png) [@nikolayandr](https://forum.datomic.com/u/nikolayandr)\
**Post date:** [September 27, 2022, 3:45pm UTC](https://forum.datomic.com/t/db-pull-and-lookup-refs-issue/2130/10 "2022-09-27T15:45:12Z")

</div>

Looks like the main idea is clear: to reduce network communication with the peer server but it leads to some issues with getting data from peer server. E.g. we do not know **basis t** of the last available DB on peer server but we know that the data is present, so we do pull

```auto
(def db (d/db conn)
(d/pull db someId pattern)

```

But since we can’t make sure **(d/db conn)** returns the newest DB from the peer server we need to do some workaround

```auto
(def db (d/db conn) # even peer server has db with basis-t e.g. 1002 this call returns db with basis t 1001
(d/pull db someId pattern) # this call returns nil but when this call is executed we see conn has updated with the newest basis-t so the next call returns necessary data
(d/pull db someId pattern)

```

I believe would be a good API for getting DB from connection would allow passing some boolean flag which allows forcing getting of the last available DB from the peer server
