Question

I am using Clojure with korma to store Clojure maps in a Mysql database, e.g storing maps with this structure:

(defentity users
  (table :user)
  (entity-fields :email 
                 :password 
                 :name))

In a table with four columns id (implicitly defined), email, password and name.

So, when I call this piece of code everything works fine:

(insert users (values {:email "john@example.com"
                       :password "____hashed_passw___"
                       :name "J"}))

The problem I have is that my maps may contain some keys which do not have their corresponding columns in the database (and I do not want to persist these values). For example:

(insert users (values {:email "john@example.com"
                       :password "____hashed_passw___"
                       :name "J"
                       :something "else"}))

Would throw an error MySQLSyntaxErrorException Unknown column 'something' in 'field list'.

Ideally, what I would like to know is if there is an option somewhere in Korma to ignore the extra keys present in the map. The problem could be solved by dissoc-ing all of them on save, but I'd prefer to find out if there is a built in way to do it before I do that (or any other better idea).

Was it helpful?

Solution

No, you have to remove extra key/value pairs from the map. Arguably Korma could be made to remove them, based on defentities.

I suggest you either bring it up on https://github.com/korma/Korma as a feature request or look into adding it yourself. Chris Granger, the maintainer, is easy to talk to and would probably consider your idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top