Home > Blockchain >  Error "No such namespace: jdbc" when requiring next.jdbc in Clojure
Error "No such namespace: jdbc" when requiring next.jdbc in Clojure

Time:05-22

I'm running into require and or next.jdbc errors when I attempt to create a postgres database:

Unhandled java.io.FileNotFoundException
   Could not locate next/jdbc__init.class, next/jdbc.clj or next/jdbc.cljc on
   classpath. 

and the following. Which appears anytime I attempt to call jdbc functions within the cljblog.db namespace.

1. Caused by java.lang.RuntimeException
   No such namespace: jdbc
   

Here's my db setup:

(ns cljblog.db)
(require '[next.jdbc :as jdbc])

(def db
  {:dbtype "postgresql"
   :dbname "cljblog"
   :host "localhost"
   :user "postgres"
   :password "postgres"})

(def ds (jdbc/get-datasource db))

(def ds (jdbc/get-datasource db))
(def conn (jdbc/get-connection ds))

(jdbc/execute! conn ["
-- postgresql version
drop table if exists posts?;
create table posts (
  id int,
  title varchar(255),
  body text,
author varchar(25)
"])

(jdbc/execute! conn ["
insert into posts(title,body,author)
  values('Working with postgres',
'this is my first attempt at working
with postgres in Clojure', 'Sam Dees')"])

(def result-set
  (jdbc/execute!
   conn
   ["select * from posts"]))

and the project.clj

(defproject cljblog "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :min-lein-version "2.0.0"
  :dependencies [[org.clojure/clojure "1.10.0"]
                 [compojure "1.6.1"]
                 [ring/ring-defaults "0.3.2"]
                 [hiccup "1.0.5"]
                 [com.github.seancorfield/next.jdbc "1.2.780"]
                 [org.postgresql/postgresql "9.4-1201-jdbc41"]]
  :plugins [[lein-ring "0.12.5"]]
  :ring {:handler cljblog.handler/app}
  :repl-options {:init-ns clj-jdbc.core}
  :profiles
  {:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
                        [ring/ring-mock "0.3.2"]]}})

*I've truncated the full error messages for brevity. *postgres is installed with an active server running on the system

CodePudding user response:

Assuming you restarted your REPL after adding those dependencies, your ns form should look like this:

(ns cljblog.db
  (:require [next.jdbc :as jdbc]))

In addition, you have:

  :repl-options {:init-ns clj-jdbc.core}

but you didn't show us that namespace (in the file src/clj_jdbc/core.clj) which I suspect is where the error is really occurring.

CodePudding user response:

You may wish to clone this demo project using Postgres in a Docker container along with next.jdbc for access:

https://github.com/cloojure/demo-jdbc-next

Follow along the instructions from the README and it should work right away. There are also examples of using jdbc.next with the H2 database.

All of the action is in the test namespaces. The file test/tst/demo/jdbc_postgres.clj starts out like so:

(ns tst.demo.jdbc-postgres
  (:use demo.core tupelo.core tupelo.test)
  (:require
    [next.jdbc :as jdbc]
    [next.jdbc.result-set :as rs]
    [next.jdbc.sql :as sql]
    ))

; ***** change this value to `false` to disable PostgreSQL unit tests *****
(def postgres-enable true)

;---------------------------------------------------------------------------------------------------
(when postgres-enable

  (def db-info {:dbtype   "postgres"
                :dbname   "example"
                :user     "postgres"
                :password "docker"
                })

  (def ds (jdbc/get-datasource db-info))

  ; NOTE: ***** Must MANUALLY  create DB 'example' before run this test! *****
  ;       In PSQL, do `create database example;`.  See the README.
  (dotest
    (jdbc/execute! ds ["drop table if exists address"])
    (let [r11 (jdbc/execute! ds ["
                create table address (
                  id      serial primary key,
                  name    varchar(32),
                  email   varchar(255)
                ) "])
          r12 (jdbc/execute! ds ["
                insert into address(name, email)
                  values( 'Homer Simpson', '[email protected]' ) "])
          r13 (jdbc/execute! ds ["select * from address "])
          ]
      (is= r11 [#:next.jdbc{:update-count 0}])
      (is= r12 [#:next.jdbc{:update-count 1}])
      (is= r13 [#:address{:id 1, :name "Homer Simpson", :email "[email protected]"}]))

<snip>

I've noticed that you seem to have something wrong in your project.clj. Mine looks like:

  :dependencies [
                 [com.h2database/h2 "1.4.200"] ; #todo cannot upgrade yet or crash!
                 [hikari-cp "2.14.0"]
                 [org.clojure/clojure "1.11.1"]
                 [org.clojure/test.check "1.1.1"]
                 [org.postgresql/postgresql "42.3.5"]
                 [prismatic/schema "1.2.1"]
                 [com.github.seancorfield/next.jdbc "1.2.780"] 
                 [tupelo "22.05.20"]
                 ]

so the dependency on org.postgresql/postgresql may be part of the problem.

  • Related