pg icon indicating copy to clipboard operation
pg copied to clipboard

Cannot call server function: pg: can't find column=xxx

Open howesteve opened this issue 4 years ago • 1 comments

Hello,

I must be doing something silly, because I just cannot call a simple server function using go-pg. I have this server function which I call with user data, it logs the user in and returns session values

howe=# \df login 
                       List of functions
 Schema | Name  | Result data type | Argument data types | Type 
--------+-------+------------------+---------------------+------
 public | login | json             | req jsonb           | func
(1 row)

On psql it works flawlessly:

 howe=# select login('{"user":"[email protected]","password":"xxx","session_timeout":86400}');
                                                                                                                                     login   
                                                                                                                                  
---------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
 {"session": {"id": "4ace8068-a103-456c-b433-8331cfa7c823", "role": "user_6", "company": "company_1", "created": "2020-03-26T15:05:05.211281"
, "expires": null, "channels": [], "metadata": {"role_id": 6, "company_id": 1}, "last_seen": "2020-03-26T15:05:05.211281-03:00"}}

But if I call it with the same parameters using go-pg, and trying to unmarshal the response into a structure such as this:

type LoginResponse struct {
	Session *Session `json:"session,omitempty"`
	Error    error  `json:"error,omitempty"`
	// Login    *Session `json:"login,omitempty"`
}

, I get this error message:

<nil> DBG message handling error error="pg: can't find column=login in model=LoginResponse (try discard_unknown_columns)"

Actually if I uncomment the LoginResponse.Login field, which is there just for debugging this issue, it returns successfully with no error message (but serialization is incorrect as show below):

{"login":{"Role":null}}

So if I'm getting right, go-pg is making assumptions about the format of the response, based on the name of the function being called? How can I fix this ?

Thanks, Howe

howesteve avatar Mar 26 '20 18:03 howesteve

It should be

type Resp struct {
    Login LoginResponse
}

and you should scan into that struct. If it does not work - you will have to provide a program that reproduces the problem.

vmihailenco avatar Mar 27 '20 06:03 vmihailenco