Giter Club home page Giter Club logo

Comments (12)

sthomp avatar sthomp commented on July 24, 2024 2

Thanks @rcanepa I'm also doing something similar

from hugsql.

Ramblurr avatar Ramblurr commented on July 24, 2024 2

@csummers Is this feature supposed to work for UPDATE statements too? Postgres also supports this.

from hugsql.

csummers avatar csummers commented on July 24, 2024 1

This happens because statements executed with execute (:! in HugSQL) are not expected to return result sets. You can get around this issue by using :? for your insert w/ returning statements.

However, I'm going to keep this open. The HugSQL docs should indicate this, I should add a test to show this, and I'm thinking about adding a command type that would indicate this. Also, this is tangentially related to issue #8.

from hugsql.

rcanepa avatar rcanepa commented on July 24, 2024 1

Hi @sthomp, I use that pattern (:i*:fields) in combination with something like this:

(s/defn create-office :- {s/Keyword s/Uuid}
  [db :- ConnectionMap
   office :- schemas/Office]
  (let [office (select-keys office schemas/office-db-fields) ;; remove read only fields
        fields (map name (keys office)) ;; get fields as strings
        values (vals office) ;; get values
        sql-args {:fields fields
                  :values values}]
    (execute! db :insert-office sql-args)))

from hugsql.

robinheghan avatar robinheghan commented on July 24, 2024

I can confirm this bug. I noticed it yesterday. My exception was "Received result when none was expected"

Using the latest version of hugsql, and funcool/clojure.jdbc adapter.

from hugsql.

rcanepa avatar rcanepa commented on July 24, 2024

@csummers great!. I can confirm that it solves the problem. All I had to do was change my query to something like this:

-- :name insert-contact! :? :1
-- :doc Insert a contact record.
INSERT INTO contacts
(:i*:fields)
VALUES
(:v*:values)
RETURNING id;

Thanks!

from hugsql.

csummers avatar csummers commented on July 24, 2024

As of the 0.4.0, there is a :command type of :returning-execute, or :<! for short. This still executes as a query like :?, but provides a bit more semantic value. See Insert for an example.

from hugsql.

rcanepa avatar rcanepa commented on July 24, 2024

Excellent!... I will update my code and let you know if I something doesn't work!... Keep the good work!!

from hugsql.

sthomp avatar sthomp commented on July 24, 2024

Just came across this issue and wanted to suggest adding an example to the Insert examples of how to insert with dynamic columns (ie: the (:i*:fields) notation used above). All the current examples use fixed columns.

from hugsql.

csummers avatar csummers commented on July 24, 2024

Thanks for the suggestion. I could add a couple of examples to this effect. I think a likely scenario instead of the separate fields and values vectors would use a hashmap of {"field1" "value1"...}. This is hinted at with the generic update example in the Clojure Expressions portion of the docs.

from hugsql.

sthomp avatar sthomp commented on July 24, 2024

That would be really nice. The {field value .. syntax is probably the best feature about Korma that other libraries are missing

from hugsql.

byrongibby avatar byrongibby commented on July 24, 2024

Works with upsert using INSERT ON CONFLICT statement as well, if there was any doubt.

from hugsql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.