Best approach to update sql table with many optional fields with different types?
See original GitHub issueI am trying out this lib on a project with Giraffe.
One use case is that a web app needs to submit an update request with a dozen fields that are mostly optional. The request comes in in JSON through JS fetch. The REST api handler will bind the payload to a record with optional fields defined as int option or string option. If a field comes in as None, then the update sql should skip the field.
Is there a recommended approach to model the request payload and build up the update sql in a unified way?
It is easy with dynamical language such as js or Clojure. But I am not sure if there is a good way with F#.
Thanks in advance and my apology if it is a naive question. I did a search but couldn’t find out good answers.
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Update multiple rows with different, optional values for ...
I want to be able to update multiple columns of multiple rows at a time, but not necessarily to update every column of...
Read more >What is the best way to store optional values in table?
The column type is varchar (max) . In other words, this column is to store values that are certain business characteristics of the...
Read more >SQL Update statement Performance Tips
Best practices to improve SQL update statement performance ... The query we will tackle below updates all rows in a heap table with...
Read more >How to handle a lot of optional columns in a large table?
From a design perspective using sharding the contents are split amongst multiple tables based on the partitioning scheme but you query it as...
Read more >How to Update Multiple Columns in Single ...
In this article, we will see, how to update multiple columns in a single statement in SQL. We can update multiple columns by...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thanks. Would try Analyzer later.
Well, there are a couple of reasons. Mainly because when you couple types (code) to database tables, that doesn’t necessarily make your code “type-safe” because now changes in the code break the application if these changes aren’t also reflected the database. This is something that the compiler cannot enforce so although your program compiles and maybe your unit tests will work too, the program will break in production because it relied on types being in sync with the database.
Another reason not to use reflection is to be able to easily map table columns to other types, for example reading a
jsonbcolumn into string first, then converting it toJToken(from Newtonsoft.Json library) or readingtimestampasZonedDateTimewithout extra plugins.Finally, using this methodology of writing the data access layer makes the code very simple and allows for tools to better work with it: checkout Npgsql.FSharp.Analyzer
Hope this helps, let me know if you have more questions 😄