Skip to content

"ERROR: null value in column" when trying to insert into the database #8

@AndreaCrotti

Description

@AndreaCrotti

So I'm just trying to insert data into the database using postgres and next.jdbc, but when it actually tries to write the data the values in the foreign key fields are always NULLS.

I thought it was related to my model but I get the same behaviour with the sample code from the README, it works fine for the mock db but not for a real Postgres db.
If I look at the output from generate I see that it's all correct, but not when I try to insert data, any idea why it could be wrong?

(def id-atom (atom 0))
(def monotonic-id-gen
  (gen/fmap (fn [_] (swap! id-atom inc)) (gen/return nil)))

(def ID
  [:and {:gen/gen monotonic-id-gen} pos-int?])

(def User
  [:map
   [:user/id ID]
   [:user/username string?]])

(def Post
  [:map
   [:post/id ID]
   [:post/created_by_id pos-int?]
   [:post/content string?]])

(def potato-schema
  {:user {:prefix   :u
          :generate {:schema User}
          :fixtures {:table-name "metagross.users"}}

   :post {:prefix    :p
          :generate  {:schema Post}
          :fixtures  {:table-name "metagross.posts"}
          :relations {:post/created_by_id [:user :user/id]}}})


(defn unwrap-connection
  [& _args]
  (.unwrap (postgres/connection) Connection))

(def potato-pg
  {:schema   potato-schema
   :generate {:generator mg/generate}
   :fixtures (merge dnj/config
                    {:get-connection unwrap-connection
                     :close-connection (fn close-connection [& _args]
                                         (.close (unwrap-connection)))})})

The code is pretty much just this, I had to redefine get-connection, but I don't think that's the problem, it connects just fine it just seems to lose the data when trying to write out.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions