1

The clojure.contrib.sql module has a create-table function that takes the table name and a list of specifications, like this:

(sql/create-table :services
                  [:id :serial "PRIMARY KEY"]
                  [:service_name :varchar "NOT NULL"]
                  [:pass_hash :varchar "NOT NULL"]
                  [:token :varchar "NOT NULL"])

If I'm reusing the same columns again and again, is there a way to define something like this?

(def same-columns 
                  [:id :serial "PRIMARY KEY"]
                  [:service_name :varchar "NOT NULL"]
                  [:pass_hash :varchar "NOT NULL"]
                  [:token :varchar "NOT NULL"])

When I tried running that in the REPL I got an error, because it passes too many arguments to def.

1 Answer 1

3

You could probably use apply for this:

(def same-columns [[:id :serial "PRIMARY KEY"]
                   [:service_name :varchar "NOT NULL"]
                   [:pass_hash :varchar "NOT NULL"]
                   [:token :varchar "NOT NULL"]])

(apply sql/create-table 
       :services 
       same-columns)

If you have other columns you can add those as well:

(apply sql/create-table 
       :services
       [:some-column :varchar "NOT NULL"]
       same-columns)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.