tonsky / tonsky/datascript

Cannot refer to a unique ref with a map

Open
#147 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Clojure
Stars
5.8k
Forks
318
PR merge metrics
No merged PRs in 30d

Description

Use case:

  • I want to be able to describe 1-to-1 relationships in my schema.
  • My solution is to create unique refs in my schema.

Did:

  • Added an attribute :foo to my schema with both :db/valueType :db.type/ref and :db/unique db.unique/identity.
  • Transacted an entity associating :foo to a map (ref)

Expected:

  • :foo to associated with the reference, which is unique in the db.
  • Refs can be unique and used in this way in Datomic.

Happened:

  • Error: #error {:message "Expected number or lookup ref for entity id, got {:db/id 1}", :data {:error :entity-id/syntax, :entity-id {:db/id 1}}}

Code to repro:

(require '[datascript.core :as d])

(def conn (d/create-conn {:foo {:db/valueType :db.type/ref 
                                :db/unique    :db.unique/identity}}))
(d/transact conn [{:db/id 1}{:foo 1}]) ;; OK, when referencing with a number

(def conn (d/create-conn {:foo {:db/valueType :db.type/ref 
                                :db/unique    :db.unique/identity}}))
(d/transact conn [{:db/id 1}{:foo {:db/id 1}}]) ;; throws, when using a map

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the supplied Clojure reproduction using d/transact first, then trace how transaction handling processes ref entity IDs and unique lookup refs. Done means the second transaction accepts {:db/id 1} as a reference and the reproduction no longer throws the reported :entity-id/syntax error.

Written by the indexing model from the issue text.

Assessment

Tech stack
clojure
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.