pgtyped icon indicating copy to clipboard operation
pgtyped copied to clipboard

Incorrect column name for unnamed column

Open golergka opened this issue 4 years ago • 1 comments

I have the following query:

const checkBlacklist = sql<ICheckBlacklistQuery>`
	SELECT COUNT(*) > 0
	FROM user_blacklist
	WHERE
		(user_id = $userID AND target_user_id = $targetUserID)
		OR (user_id = $targetUserID AND user_id = $userID)
`

In this query, I don't give column a name, and by default, PostgreSQL returns column named ?column?. Unfortunately, pgtyped uses this exact name, without escaping or checking, for the generated type:

/** 'CheckBlacklist' return type */
export interface ICheckBlacklistResult {
  ?column?: boolean;
}

Which is invalid Typescript code.

golergka avatar Aug 11 '20 10:08 golergka

Thanks for reporting this. It should certainly be escaped. Meanwhile a temporary solution will be to name the column:

const checkBlacklist = sql<ICheckBlacklistQuery>`
	SELECT COUNT(*) > 0 AS is_user_blacklisted
	FROM user_blacklist
	WHERE
		(user_id = $userID AND target_user_id = $targetUserID)
		OR (user_id = $targetUserID AND user_id = $userID)
`

adelsz avatar Aug 15 '20 22:08 adelsz

Anonymous return fields are now explicitly forbidden and generate an error. Please consider naming them instead.

adelsz avatar Jan 29 '23 01:01 adelsz