Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

postgres :alter-column documentation produces incompatible queries. #536

Closed
Tillaert opened this issue Jul 18, 2024 · 2 comments
Closed
Assignees
Labels
documentation I need to write something up!

Comments

@Tillaert
Copy link

According to the honeysql postgres documetation:

This statement is given as an example.

user=> (-> (alter-table :fruit)
           (alter-column :name [:varchar 64] [:not nil])
           sql/format)
["ALTER TABLE fruit ALTER COLUMN name VARCHAR(64) NOT NULL"]

However, this creates code which is not compatible with postgres.

Postgres expects

ALTER TABLE fruit ALTER COLUMN name TYPE VARCHAR(64)

and

ALTER TABLE fruit ALTER COLUMN name SET NOT NULL

(See: https://www.postgresql.org/docs/current/sql-altertable.html )

This can be achieved with these queries:

{:alter-table [:table], :alter-column [(:column :set [:not nil])]}

and:

{:alter-table [:table], :alter-column [(:column :type [:varchar 64])]}

@seancorfield
Copy link
Owner

seancorfield commented Jul 18, 2024

Your alternatives do not work:

user=> (sql/format {:alter-table [:table], :alter-column [(:column :set [:not nil])]})
["ALTER TABLE table ALTER COLUMN not NULL"]

But the following do work:

user=> (-> (alter-table :fruit)
           (alter-column :name :type [:varchar 64])
           sql/format)
["ALTER TABLE fruit ALTER COLUMN name TYPE VARCHAR(64)"]
user=> (-> (alter-table :fruit)
           (alter-column :name :set [:not nil])
           sql/format)
["ALTER TABLE fruit ALTER COLUMN name SET NOT NULL"]
user=>

The raw DSL for that last one is:

{:alter-table [:fruit], :alter-column [:name :set [:not nil]]}

@seancorfield seancorfield self-assigned this Jul 18, 2024
@seancorfield seancorfield added the documentation I need to write something up! label Jul 18, 2024
@Tillaert
Copy link
Author

You are correct. My apologies for the mistake.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
documentation I need to write something up!
Projects
None yet
Development

No branches or pull requests

2 participants