簡體   English   中英

Hugsql 無法讀取我的 sql 文件

[英]Hugsql can not read my sql file

我真的在這里迷路了。 我有一個非常簡單的應用程序。 它所做的只是將用戶插入到我的數據庫中的用戶表中。 我使用 Postgres。 代碼是

(ns signupper.db (:require [hugsql.core :as hugsql]))
(hugsql/def-db-fns "sql/q.sql")

在 db.clj 所在的目錄中,我創建了一個名為 sql 的目錄,其中有一個名為 q.sql 的文件。

當我運行 REPL 並輸入 (require '[signupper.db :as db]) 時,我收到以下錯誤消息:

CompilerException clojure.lang.ExceptionInfo: Can not read file: sql/q.sql {}, compiling:(signupper/db.clj:4:1) 

任何人有任何想法?

謝謝。

您的sql目錄需要在路徑上。 請檢查您的project.clj文件,在resource-paths ,並驗證您的sql目錄可通過其中所述的路徑之一訪問。

如果沒有,您可以移動sql目錄或將路徑包含在resource-paths條目中。

如果您使用Leiningen,您應該將:resource-paths鍵下的 sql 文件夾添加到您的project.clj如下所示:

(defproject test-project "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.8.0"]]
  :main ^:skip-aot test-project.core
  :target-path "target/%s"
  :resource-paths ["sql" "resources"]  ; <-- here
  :profiles {:uberjar {:aot :all}})

Hugsql 需要相對於類路徑上的目錄的路徑。 (這與 Clojure 命名空間相同。)

所以,你的代碼應該是這樣的:

(ns signupper.db (:require [hugsql.core :as hugsql]))

(hugsql/def-db-fns "signupper/db/sql/q.sql")

請參閱文檔中的示例: https : //www.hugsql.org/#start-sql

我的經驗:

如果你的路徑是這樣的:

"/home/user/Desktop/sql/q.sql"

project.clj 一定是這樣的 -->

:resource-paths ["/home/user/Desktop/sql" "other resources"]

和 core.clj -->

(hugsql/def-db-fns "q.sql")

但是如果你寫在core.clj

(hugsql/def-db-fns "/home/user/Desktop/sql/q.sql")

電腦是這樣看的:

"/home/user/Desktop/sql/home/user/Desktop/sql/q.sql"

剛剛有同樣的問題。

顯然,函數def-db-fns從文件夾src而不是項目的根目錄開始路徑。

我認為您可以解決此問題的一種方法是將您的文件放在 src 目錄中,並使用(hugsql/def-db-fns "q.sql")調用該函數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM