Fork me on GitHub
#honeysql
<
2020-06-10
>
codonnell00:06:47

Ah well. If you're set on using honeysql for ddl, you could probably adapt the definitions from postgres.

avi00:06:15

I’ll take a look. Thank you!

seancorfield00:06:07

@aviflax Outside of the tests in clojure.java.jdbc, I've never used the DDL helpers -- they just don't add enough.

codonnell01:06:23

I don't use them, either. I tend to put ddl in raw sql migration files.

seancorfield01:06:27

@codonnell Yeah, that's the route we've gone at work, so it's easier to roll a database up for testing to a specific "version".

avi01:06:13

That makes sense. Thanks! FWIW, I was just hoping to find some kind of “standardized” way to represent a create table statement as a data structure rather than as a string, because I am working on a function that will eventually return something that has generated create table statement in its metadata, and I want to be able to write equality checks in my tests without worrying about string formatting. I whipped this up on the fly, thinking this might work:

[:create-table
 "technologies"
 "name varchar(255) not null primary key"
 "links-main text"]
But my SQL is so rusty I don’t have much confidence in something so simple being actually viable. 😅

seancorfield01:06:36

I would probably use a hash map: {:create-table :technologies :columns ["name varchar(255) not null primary key", "links_main text"]}

seancorfield01:06:37

then you can (clojure.string/join ", " ...) on the columns and then it's just (str "CREATE TABLE" (:create-table data) "(" column-string ")")

avi01:06:29

I like that. More forwards-compatible. Thanks!

kwrooijen14:06:25

Hello. I'm trying to create a new row, and return the generated primary key (or complete row).

(-> (insert-into :person)
    (values [{:name "foo"}])
    (sql/format)
    (->> (jdbc/execute! *database*)))
This inserts the row as expected, but returns (1) instead of the new row. Is there any way I can get the primary key? I also saw that there was a jdbc/insert! , but this takes a map instead of an SQL string. Meaning I can't use honeysql with this. Any tips?

gon15:06:23

you can use 'returning id' clause at the end of the insert if your db implements it

kwrooijen16:06:45

Ah ok, I also need to use jdbc/query instead of jdbc/execute! if I want it to return data

seancorfield17:06:15

Use :return-keys true

seancorfield17:06:38

(As an option passed to execute!