Fork me on GitHub
#honeysql
<
2022-06-29
>
bortexz15:06:34

I’m trying to generate SQL to create an index in postgresql, but it seems that the (add-index) fn only works for :primary-key and :unique, has anyone faced this problem? In postgresql docs, I can only find the CREATE INDEX clause to create new indexes on certain column(s), but not the alter table … add index …. as generated by honeysql

seancorfield15:06:08

https://github.com/seancorfield/honeysql/issues/348 -- TL;DR: it's hard to support generically because it's almost entirely database-specific.

bortexz16:06:44

I tried to execute the SQL generated directly on postgres, but it seems that syntax is not supported:

type "look" does not exist

seancorfield16:06:35

What exact SQL are you trying to run there?

bortexz16:06:43

ALTER TABLE fruit ADD INDEX look(appearance)
^ This one, having a table fruit with appearance column

bortexz16:06:00

Using postgresql 14

seancorfield16:06:36

So it's expecting a type of index where you currently look. Let me look at the PG docs...

seancorfield16:06:56

Try adding {:quoted true} to the format call. All the examples I can find of alter table .. add index .. for PG seem to use quoted names.

seancorfield16:06:57

DDL is a giant mess and varies dramatically from database to database which is why HoneySQL hadn't used to support any DDL. I'm gradually adding the more common pieces but some of them are just really hard to add in any way that works across multiple databases.

bortexz16:06:45

Adding :quoted true didn’t work, it still complains. Even executing the SQL with quotes doesn’t work

ALTER TABLE fruit ADD INDEX 'look'('appearance')
(now a syntax error) I imagine it’s a mess and by the issue you posted looks like create index might not be that general, for now I will create and register the clause myself thanks for the help 🙂

seancorfield16:06:56

It should be double quotes, not single.

seancorfield16:06:12

ALTER TABLE "fruit" ADD INDEX "look" ("appearance")

seancorfield16:06:38

That syntax is used in several tutorials I found for PostgreSQL so I assumed it works -- I don't use PG myself.

seancorfield16:06:30

I also don't currently have DDL tests for this so I should add those.

bortexz16:06:45

still complains about type “look” not existing

seancorfield16:06:11

OK. Thanks for testing that. The tutorials must be wrong (no surprise, I guess).

bortexz16:06:39

I also found a tutorial that made use of add index in postgres, wondering if they used to support it? But when looking through the docs of alter table on postgres 14 I couldn’t find generic add index supported, only for primary keys, constraints, uniques etc