简体   繁体   English

测试错误:如何使用:id?

[英]Test error: how to use :id?

I'm trying to do this for the first time in luminus , h2 , hugsql and clojure . 我正在尝试使用luminush2hugsqlclojure进行第一次此操作。

The insert statement works fine when entered in a SQL client connected to the h2 database, but fails in code. 在连接到h2数据库的SQL客户端中输入时, insert语句可以正常工作,但是在代码中失败。 It seems it has to do with the WHERE id = :id clause of the get-assessor query, but can't find a way to do this. 似乎与get-assessor查询的WHERE id = :id子句有关,但是找不到解决方法。

In file ./resources/sql/queries.sql 在文件./resources/sql/queries.sql

-- :name get-assessor :? :1
-- :doc retrieve a assessor given the id.
SELECT * FROM assessores
WHERE id = :id

-- :name save-assessor! :n
-- :doc creates new assessor
INSERT INTO assessores
    (first_name,
    last_name,
    email,
    phone,
    address1,
    address2,
    post_code,
    post_code_city,
    birth_date,
    tax_number,
    to_buy,
    to_sell,
    to_career,
    first_message,
    is_active)
VALUES (:first_name,
    :last_name,
    :email,
    :phone,
    :address1,
    :address2,
    :post_code,
    :post_code_city,
    :birth_date,
    :tax_number,
    :to_buy,
    :to_sell,
    :to_career,
    :first_message,
    :is_active)

In file: ./test/db/core.clj 在文件中: ./test/db/core.clj

(deftest test-assessores
  (jdbc/with-db-transaction [t-conn *db*]
    (jdbc/db-set-rollback-only! t-conn)
    (let [timestamp (java.util.Date.)]
      (is (= 1 (db/save-assessor!
                 t-conn
                 {:first_name "Bob"
          :last_name            "Singer"
          :email                "lpe@gmail.com"
          :phone                "888232418"
          :address1             "10 st"
          :address2             "VNC"
          :post_code            "9990-990"
          :post_code_city       "Cer"
          :birth_date           "1962-06-06"
          :tax_number           204559449
          :to_buy               true
          :to_sell              false
          :to_career            false
          :first_message        "how to buy?"
          :is_active            true}
                 {:connection t-conn})))

      (is (=
           {:first_name "Bob"
          :last_name            "Singer"
          :email                "lpe@gmail.com"
          :phone                "888232418"
          :address1             "10 st"
          :address2             "VNC"
          :post_code            "9990-990"
          :post_code_city       "Cer"
          :birth_date           "1962-06-06"
          :tax_number           204559449
          :to_buy               true
          :to_sell              false
          :to_career            false
          :first_message        "how to buy?"
          :is_active            true}
           (-> (db/get-assessor t-conn {})
               (first)
               (select-keys [:first_name ])))))))

The messages returned (truncated) are: 返回(截断)的消息是:

org.h2.jdbc.JdbcSQLException: Method is only allowed for a query. Use execute or executeUpdate instead of executeQuery; SQL statement:
                              INSERT INTO assessores
                                (first_name,
                                last_name,
                                email,
                                phone,
                                address1,
                                address2,
                                post_code,
                                post_code_city,
                                birth_date,
                                tax_number,
                                to_buy,
                                to_sell,
                                to_career,
                                first_message,
                                is_active)
                              VALUES ( ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ) [90002-192]
                SQL: "INSERT INTO assessores\n\t(first_name,\n\tlast_name,\n\temail,\n\tphone,\n\taddress1,\n\taddress2,\n\tpost_code,\n\tpost_code_city,\n\tbirth_date,\n\ttax_number,\n\tto_buy,\n\tto_sell,\n\tto_career,\n\tfirst_message,\n\tis_active)\nVALUES ( ? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? )"
           SQLState: "90002"
          errorCode: 90002
    originalMessage: "Method is only allowed for a query. Use execute or executeUpdate instead of executeQuery"

ERROR in (test-assessores) (core.clj:4617)

clojure.lang.ExceptionInfo: Parameter Mismatch: :id parameter data not found.

Testing orio.test.handler
(...)
Ran 2 tests containing 4 assertions.
0 failures, 2 errors.
Tests failed.

How to fix this? 如何解决这个问题?

-- :name save-assessor! :n

should be 应该

-- :name save-assessor! :! :n

see: https://www.hugsql.org/#command 参见: https : //www.hugsql.org/#command

the command defaults to :? 该命令默认为:? which indicates that it is a non-modifying query. 表示这是一个非修改查询。 For insert/update statements, a :! 对于插入/更新语句,请使用:! should be used in the definition. 应该在定义中使用。

Edit: After some more information has been given -- it looks like (get-assessor) is looking for a parameter of :id (as can be seen by your definition in the HUGSql file. 编辑:给出更多信息后-看起来(get-assessor)正在寻找:id的参数(如您在HUGSql文件中的定义所示)。

I would create a new query in that file. 我将在该文件中创建一个新查询。 Something like (get-first-assessor) (get-first-assessor)

-- :name get-first-assessor :? :1 -- :doc get the first assessor in the system, ordred by id SELECT * FROM assessores ORDER BY id ASC LIMIT 1

Then replace your call to (get-assessor t-conn {}) with (get-first-assessor t-conn {}) in your test 然后在测试中将呼叫(get-assessor t-conn {})替换为(get-first-assessor t-conn {})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM