Replies: 4 comments 5 replies
-
|
Thank you! |
Beta Was this translation helpful? Give feedback.
-
|
I've looked into this and there's not much I can do about it I'm afraid: Squirrel gets the types from the Postgres server by asking it to prepare each query; what the Postgres server does is - once it has prepared a query - it sends back to squirrel the OID (identifiers) for the types of each argument in the query. However, this ID is only telling us what type the argument is, but nothing about how it's used or whether it's nullable or not. So would have to be supported by Postgres first. In the meantime you could write two different queries: one that inserts all values and one that inserts just one when the other is null: -- insert_book.sql
insert into book(title, author) values ($1, $2);
-- insert_book_with_no_author.sql
insert into book(title, author) values ($1, null);And in your Gleam code you'd pick which one to call depending on wether the author is there or not: case author {
Some(name) -> sql.insert_book(db, title, author)
None -> sql.insert_book_with_no_author(db, title)
}Luckily Squirrel makes it easy to write and use multiple queries, so as long as you don't have an explosion of combinations of not-null/null values it should be easily manageable :) |
Beta Was this translation helpful? Give feedback.
-
|
I'm ingesting data into a postgres db where there are 7 nullable fields. And this is just the first data feed I've started working on.. I'll be ingesting from ~6 more feeds, all of which have similar issues with nullable fields. Couldn't we advise squirrel of the fields that might be null at compile time? Obviously the generated code knows that the values are None at runtime... |
Beta Was this translation helpful? Give feedback.
-
|
How do you feel about a If no issues with this approach, I could submit a PR. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Steps to reproduce:
Table:
Query:
This is how the generated code looks like:
With the
namecolumn being nullable i would expect the parameter forarg_2to look like this:From looking through the code, it seems that the check if column is nullable happens only if the query returns something, which doesn't happen for insert queries. So when they are parsed we get only the types of columns.
Beta Was this translation helpful? Give feedback.
All reactions