question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update to array records with new v3 `setColumn` query

See original GitHub issue

I’m looking to add a new value to a column value of uuid[] and I’m running into scoping issues. I’m not able to get the current values of that column when trying to add a new value to it. This stems from the add to LINQ style column update from #40. I’d be happy to update the documentation like discussed in #50 and this use case as well when this is figured out. I wasn’t really able to find any complex updates from other user searching the internet with the new syntax.

CREATE TABLE Users (
    id uuid NOT NULL PRIMARY KEY,
    items uuid[] NOT NULL DEFUALT ARRAY[]::uuid[] )
type User = 
    { Id: Guid
      Items: Guid list }

let userTable = table<User>

let addItem (conn: IDbConnection) (userId: Guid) (itemId: Guid) =
    update {
        for u in userTable do
            setColumn u.Items (List.Cons(itemId, u.Items)
            where (id = userId)
    }
error FS0039: The value or constructor 'u' is not defined

The equivalent SQL command I’m trying to emulate is

UPDATE users
SET items = array_append(items, @item)
WHERE id = @id

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JordanMarrcommented, Dec 11, 2021

I think it might be as easy as this:

open Dapper

let addItem (conn: IDbConnection) (user: User) (item: Guid) =
    conn.Execute(
        """
        UPDATE users
        SET items = array_append(@items, @item)
        WHERE id = @id")
        """, {| items = user.Items; item = item; id = user.Id |})

open Dapper is key here because Dapper adds its own extension methods (like Execute) on top of the connection.

1reaction
Evelioscommented, Dec 10, 2021

I haven’t done it with the old version. I just started to pick up this library recently using v3. If this wasn’t a feature that was supported before then I guess this isn’t actually an issue but a new use case

Read more comments on GitHub >

github_iconTop Results From Across the Web

Updating a value in an ARRAY in a BigQuery table
Try this: UPDATE itemInformation SET itemDelta = ARRAY(SELECT AS STRUCT * REPLACE(9426 AS typeId) FROM UNNEST(itemDelta)) WHERE 2424 IN ...
Read more >
SQL Update Statement — Example Queries for ...
The UPDATE statement can be used to update a single column, a larger set of records (through the use of conditions), and/or the...
Read more >
How to Modify Arrays in PostgreSQL
The most basic way to modify an array column are to overwrite all values by assigning it a new array, or to specify...
Read more >
How to Update millions or records in a table - Ask TOM
I got a table which contains millions or records. I want to update and commit every time for so many records ( say...
Read more >
How to update an array field for many records at one time?
Definitely better option how to update all these records is to use the background script (module "Scripts - Background"). So I think you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found