简体   繁体   English

在Clojure中,如何配置Korma和Ragtime使用相同的数据库?

[英]In Clojure, how do I configure Korma and Ragtime to use the same database?

I'm trying to work with databases in Clojure. 我正在尝试在Clojure中使用数据库。 At this point, I want to use Ragtime to modify the database schema itself, Korma to query and insert data, and H2 as the actual database. 此时,我想使用Ragtime修改数据库架构本身,使用Korma来查询和插入数据,并使用H2作为实际数据库。

I think I'm using them properly, but I'm getting an error when I try to use Korma to access a table. 我认为我正确使用了它们,但是当我尝试使用Korma访问表时遇到错误。

Here's my project.clj: 这是我的project.clj:

(defproject dbexplore "0.1.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.6.0"]
                 [korma "0.4.0"]
                 [com.h2database/h2 "1.4.182"]
                 [ragtime "0.3.7"]]
  :plugins [[ragtime/ragtime.lein "0.3.7"]]
  :ragtime {:migrations ragtime.sql.files/migrations
            :database "jdbc:h2:/home/zck/Documents/dbexplore/resources/db/dbexplore.db"}
  :main dbexplore.core)

So I'm importing korma, h2database, and ragtime. 所以我要导入korma,h2database和ragtime。 I'm not sure it's pointing the Ragtime migration at the proper database location. 我不确定它是否将Ragtime迁移指向了正确的数据库位置。

I created a migration file with this as the contents: 我创建了一个迁移文件,其内容如下:

create table users (id INT, first varchar(32), last varchar(32));

And then ran it: 然后运行它:

zck@zck-desktop:~/Documents/dbexplore$ lein ragtime migrate
Applying 2014-10-22-2-11-create-tables

I made a simple core.clj file that just selects everything from the users table: 我制作了一个简单的core.clj文件,该文件仅从users表中选择所有内容:

(ns dbexplore.core
  (:require [korma.db :as db]
            [korma.core]))

(def db-connection (db/h2 {:db "./resources/db/dbexplore.db"}))

(db/defdb korma-db db-connection)

(korma.core/defentity users)

(defn -main []
  (korma.core/select users))

But upon running it with lein run , I get an error: 但是在用lein run来运行它时,我得到一个错误:

Failure to execute query with SQL:
SELECT "users".* FROM "users"  ::  []
JdbcSQLException:
 Message: Table "users" not found; SQL statement:
SELECT "users".* FROM "users" [42102-182]
 SQLState: 42S02
 Error Code: 42102
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Table "users" not found; SQL statement:
SELECT "users".* FROM "users" [42102-182], compiling:(/tmp/form-init7833348906040195763.clj:1:90)

My suspicion is that I'm pointing at a different database file in the h2 call in core.clj from the one ragtime is migrating, but I'm not sure how to specify it properly. 我的怀疑是我正在从一次ragtime迁移core.clj的h2调用中指向另一个数据库文件,但是我不确定如何正确指定它。 How can I make these two libraries use the same database? 如何使这两个库使用同一数据库?

I created an issue in korma: https://github.com/korma/Korma/issues/273 我在科尔马中创建了一个问题: https : //github.com/korma/Korma/issues/273
Maybe you want to add something. 也许您想添加一些东西。

I was able to read the data from the h2 database with yesql instead of korma. 我能够使用yesql而不是korma从h2数据库中读取数据。 According to different threads it should be possible to use mysql or postgresql instead of h2, but I have not tried that. 根据不同的线程,应该可以使用mysql或postgresql代替h2,但是我还没有尝试过。

Btw. 顺便说一句。 I have successfully used datomic for my last sideprojects with none of these problems, so don't give up, just try a different setup. 我已经成功地将datomic用于我的最后一个sideproject,而这些问题都没有,所以请不要放弃,只需尝试其他设置即可。

UPDATE Please have a look at the linked bug, the maintainer posted two solutions, which both do work for me. 更新请查看链接的错误,维护者发布了两个解决方案,它们都对我有用。

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

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