繁体   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