How to update schema of adding ":db/unique :db.unique/identity" with Datomic

I had initial scheme for email, as follows:

    {:db/ident :user/email
        :db/valueType :db.type/string
        :db/cardinality :db.cardinality/one
        :db/doc "Email of the User"}

It worked until I had the need to retrieve a user by his email address.
Then I had to add :db/unique :db.unique/identity

    {:db/ident :user/email
        :db/valueType :db.type/string
        :db/cardinality :db.cardinality/one
        :db/unique :db.unique/identity
        :db/doc "Email of the User"}

I added in the scheme definition, it solved the problem of not able to retrieve user by email address.
But however, when I did transact with the update schema, I got the following error:

    :db.error/invalid-alter-attribute Error: {:db/error
       :db.error/unique-without-index, :attribute :user/email}

I tried to study Datomic’s documentation on change schema:
But I don’t know how to follow the instruction:

If there are values present for that attribute, they must be unique in the set of current database assertions.

Would you please explain to me what I’m supposed to do for the instruction?
(I’m pretty new to Datomic)

The problem has been solved by the following:

    (def tx-add-index @(d/transact conn [{:db/id :user/email
                                        :db/index true}]))
    (def tx-fix @(d/transact conn [{:db/id :user/email
                                    :db/unique :db.unique/identity}]))

where conn is a connection to the on-prem (dev) database.
Here is the helpful document:

I got the answer from @favila at Slack clojurians/datomic
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.