Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Instance of : ToRow (Values (Int, Text)) #268

Closed
delanoe opened this issue Sep 1, 2018 · 5 comments
Closed

Instance of : ToRow (Values (Int, Text)) #268

delanoe opened this issue Sep 1, 2018 · 5 comments

Comments

@delanoe
Copy link

delanoe commented Sep 1, 2018

Hello,
I am trying this example in the documentation:

-- > |] ("foo", Values [ "int4", "text" ]

I need to specify the types , fine, but I get an error :

• No instance for (ToRow (Values (Int, Text)))
    arising from a use of ‘query’

I am wondering how I should declare this instance and I am guessing there is something wrong somewhere and unexpected since the example is documented.

Do you have an example please ?

Many thanks in advance for the help.

@lpsmith
Copy link
Owner

lpsmith commented Sep 1, 2018

Does your example use a single ? parameter? Sounds like you need to wrap the Values type in an Only constructor.

@delanoe
Copy link
Author

delanoe commented Sep 1, 2018

Indeed, you are right! Using the Only constructor, it compiles now. Many thanks.

@delanoe delanoe closed this as completed Sep 1, 2018
@delanoe
Copy link
Author

delanoe commented Sep 1, 2018

It compiles but I get an SQL error: "subquery should return one column"

Then I am trying to use the function renderQuery you gave here:
#262

I can hardly use it to show the resulting query sent to the database.
May you give an example how I could debug this please ?

Here is the function:

insertDocs2 :: Connection -> IO [DocId]                                                                                                                                                                
insertDocs2 conn = query conn [sql|                                                                                                                                                                    
    WITH input_rows(typename,user_id,parent_id,name,hyperdata) AS (                                                                                                                                    
       VALUES (?)                                                                                                                                                                                      
       )                                                                                                                                                                                               
    , ins AS (                                                                                                                                                                                         
       INSERT INTO nodes (typename,user_id,parent_id,name,hyperdata)                                                                                                                                   
       SELECT * FROM input_rows                                                                                                                                                                        
       ON CONFLICT ((hyperdata ->> 'doi')) DO NOTHING -- on unique index                                                                                                                               
       RETURNING id,hyperdata                                                                                                                                                                          
       )                                                                                                                                                                                               
                                                                                                                                                                                                       
    SELECT true AS source                     -- true for 'inserted'                                                                                                                                   
         , id                                                                                                                                                                                          
         , hyperdata ->> 'doi'  as doi                                                                                                                                                                 
    FROM   ins                                                                                                                                                                                         
    UNION  ALL                                                                                                                                                                                         
    SELECT false AS source                    -- false for 'selected'                                                                                                                                  
         , c.id                                                                                                                                                                                        
         , hyperdata ->> 'doi' as doi                                                                                                                                                                  
    FROM   input_rows                                                                                                                                                                                  
    JOIN   nodes c USING (hyperdata);         -- columns of unique index                                                                                                                               
           |] (Only $ Values [ "int4","int4","int4","text","jsonb"]-                                                                                                                                   
              [ (4 :: Int ,1 :: Int,452162 :: Int, DT.pack "name", DT.pack "{}")]                                                                                                                      
              )          

Many thanks in advance.

@lpsmith
Copy link
Owner

lpsmith commented Sep 1, 2018

Yes, renderQuery would not be very useful to you. Basicall, what you would want to do is replace query with formatQuery.

@delanoe
Copy link
Author

delanoe commented Sep 1, 2018

Indeed. Now I better understand how to use formatQuery. I have solved my issue, found the right query, and I do think I have now a great workflow to work with postgresql-simple. Many thanks for this library, for your answers. Best wishes for your projects.

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

No branches or pull requests

2 participants