I'm using Postgres for the first time and am confused by an "ambiguous" error regarding my functions.
CREATE TABLE "user" (
user_id VARCHAR(40) PRIMARY KEY,
email VARCHAR(254),
phone_number VARCHAR(15) NOT NULL,
username VARCHAR(15) NOT NULL,
display_name VARCHAR(15),
rank_points INT NOT NULL,
created_on TIMESTAMP DEFAULT (TIMEZONE('UTC', NOW()))
);
I have the following function to get users by id, phone, or username.
CREATE OR REPLACE FUNCTION user_get(user_id_param VARCHAR(40) DEFAULT NULL, phone_number_param VARCHAR(15) DEFAULT NULL, username_param VARCHAR(15) DEFAULT NULL)
RETURNS TABLE(user_id VARCHAR(40), email VARCHAR(254), phone_number VARCHAR(15), username VARCHAR(15), display_name VARCHAR(15), rank_points INT, created_on TIMESTAMP) AS
$$
BEGIN
RETURN QUERY
SELECT "user".user_id, "user".email, "user".phone_number, "user".username, "user".display_name, "user".rank_points, "user".created_on
FROM "user"
WHERE
(user_id_param IS NOT NULL AND "user".user_id = user_id_param) OR
(phone_number_param IS NOT NULL AND "user".phone_number = phone_number_param) OR
(username_param IS NOT NULL AND "user".username = username_param);
END;
$$
LANGUAGE plpgsql;
My question is regarding the requirement of "user." in the SELECT as well as the WHERE cause. Without it I get an error:
ERROR: column reference "user_id" is ambiguous
Where is the ambiguity? Is it with the user_id in the RETURNS TABLE clause?