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

Support explicit casts in multi-inserts #23

Open
tonyalaribe opened this issue Jul 13, 2024 · 0 comments
Open

Support explicit casts in multi-inserts #23

tonyalaribe opened this issue Jul 13, 2024 · 0 comments

Comments

@tonyalaribe
Copy link

I have a record:

data RecordF = RecordF{
  , formatHashes :: V.Vector Text
  -- other fields
  }
  deriving stock (Generic)
  deriving anyclass (EncodeRow)

Then corresponding logic to insert this record as a multi-row insert:

bulkInsert :: [RecordF] -> Hasql.Statement () ()
bulkInsertRequestDumps rowsToInsert =
  interp
     True
     [sql| INSERT INTO tableNmae (formatHashes, otherField)
     SELECT * from ^{Hasql.toTable rowsToInsert} ON CONFLICT DO NOTHING; |]

This process works fine and nicely (my original record has 30fields, so it saves a lot of boilerplate as well.

But fields like formatHashes are never inserted correctly. I get the following error:

(ResultError (ServerError \"42804\" \"column \\\"format_hashes\\\" is of type text[] but expression is of type text\" Nothing (Just \"You will need to rewrite or cast the expression.\") (Just 407)

Logging the values format hashes appears to be a string that holds the something like:
[['abc', 'cde']] but as a string. And I imagine if I could cast it into text[] this would work fine.

INSERT INTO tableNmae (formatHashes, otherField)
     SELECT * from unnest($1::text[], $2) ON CONFLICT DO NOTHING; 

Is there a workaround to allowing me cast the fields correctly? I don't mind manually writing the numbers. But I would like to maintain the boilerplate encoding which hasql-interpolate does for me.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant