What is wrong with my WHERE condition in Postgres? [duplicate] - postgresql

This question already has answers here:
What is the difference between single quotes and double quotes in PostgreSQL?
(3 answers)
Error: Column does not exist in postgresql for update [duplicate]
(1 answer)
postgres column "X" does not exist
(1 answer)
Simple Postgresql Statement - column name does not exists
(2 answers)
Closed 2 years ago.
I am running the query SELECT * FROM app_user WHERE login_id = "john"; and getting -
ERROR: column "john" does not exist
LINE 1: SELECT * FROM public.app_user WHERE login_id = "john";
^
SQL state: 42703
Character: 48
I have also tried SELECT * FROM public.app_user WHERE login_id = "john"; and I still get the same error.
The same error also occurs with any other column but the id column(id is the only non-VARCHAR column and is the primary key).
So, SELECT * FROM app_user WHERE id = 5; is working as expected .
A snapshot of the table follows.

If you write the string in double quotes, postgres will interpret it as a column name in the WHERE clause, just use single quotes:
SELECT * FROM public.app_user WHERE login_id = 'john';

Related

SqlAlchemy Partial Unique Index not getting created [duplicate]

This question already has answers here:
sqlalchemy IS NOT NULL select
(3 answers)
Select NULL Values in SQLAlchemy
(5 answers)
sqlalchemy postgresql "Is Null" index
(1 answer)
Closed 1 year ago.
I am trying to create an partial unique index only for the column whose value is null,
this is my db model in SQL alchemy, and this is how I tried to create the partial index.
class RequestedTarget(db.Model):
__tablename__ = "example"
id = Column(UUIDType(), primary_key=True, default=uuid4)
table1_id= Column(String(), ForeignKey("table1.id"), nullable=False)
table2_id= Column(String(), ForeignKey("table2.id"))
__table_args__ = (
Index("single_root_target", table1_id, table2_id, unique=True, postgresql_where=table2_id.is_(None)),)
But when I run my program, I am still able to create two records with table1_id = x and table2_id = null. please let me know when I have gone wrong.

Postgres query is not null seems to be return nulls in the result [duplicate]

This question already has answers here:
Are PostgreSQL column names case-sensitive?
(5 answers)
Closed 1 year ago.
below is the SQL query ran in Postgres and its corresponding output.
The data type for the column 'premiseId' is text which is the equivalent of varchar (without any length specifier). So my query is how come the results contain null values in the specified column?
The Postgres version used is 10.11.
Single quotes in SQL denote string literals. 'premiseId' is not the column's name, it's a string literal. Since this literal is not null, all the rows in the table are returned. If you want to use the column in the query, you should refer to it without the quotes:
SELECT * FROM request WHERE premiseId IS NOT NULL
-- No quotes ---------------^-------^
Ah thanks for the replies!! I should have used double quotes instead as the column name has some characters that are upper case.
select * from request where "premiseId" is NOT NULL;
when trying to reproduce your issue:
create table testtable (
id integer primary key,
some_text varchar
)
insert into testtable values
(1, NULL), (2, 'abc'), (3, 'NULL'), (4, '[null]');
select * from testtable where some_text is not null
I got only expected results.
id
some_text
2
abc
3
NULL
4
[null]
as the id 3,4 are texts NULL or [null] and not the actual "value" null. Maybe an issue with your loader and the representation in the source (e.g. csv)
You should specify column names without quotes
SELECT * FROM request WHERE premiseId IS NOT NULL;
Here documentation https://www.postgresql.org/docs/current/sql-syntax-lexical.html

Can someone explain why I have this error column does not exist? [duplicate]

This question already has answers here:
Postgres error updating column data
(2 answers)
PostgreSQL "column "foo" does not exist" where foo is the value
(1 answer)
postgres column "X" does not exist
(1 answer)
Simple Postgresql Statement - column name does not exists
(2 answers)
Error: Column does not exist in postgresql for update [duplicate]
(1 answer)
Closed 2 years ago.
Insert into Customer(
Customerid,
Username,
Fname,
Lname,
Street1,
Street2,
City,
State,
Zip
) VALUES(42, “Guitarhero”, “Wes”, “Montgomery”, “Mainstreet”, “Manhattan”, “NY”, 12304);
ERROR: column "“guitarhero”" does not exist
You're using double quotes for strings instead of single quotes. Double quotes indicate a column name, and single quotes indicate a string. Try this:
Insert into Customer (Customerid, Username, Fname, Lname, Street1, Street2, City, State, Zip)
VALUES (42, 'Guitarhero', 'Wes', 'Montgomery', 'Mainstreet', 'Manhattan', 'NY', 12304);
Also note that you're using uppercase letters in your table and column names, but those will be changed to lowercase by postgres unless you put them in double quotes.

Array in postgresql [duplicate]

This question already has answers here:
Postgres: check if array field contains value?
(4 answers)
How to make a select with array contains value clause in psql
(3 answers)
Search in integer array in Postgres
(3 answers)
Closed 4 years ago.
I have a table with a column 'sample_column' which is an array.
Can anyone tell me how can I select data based on 'sample_column' in postgresql?
Example of data in sample_column: ["one","two","three"]
I want to get all data if sample_column has value "three"
Here is what I have done:
Select * from sample_table where sample_column contains 'three'
I am getting ERROR.
Any help will be appreciated.
I assume you have table as:
CREATE TABLE table_name
(
sample_column text[]
);
and you have insert data as:
insert into table_name(sample_column) values (array['one','two','three']);
insert into table_name(sample_column) values (array['yes','no']);
insert into table_name(sample_column) values (array['red','white','blue']);
now you want to find recored based on the array element:
select * from table_name where 'three' = ANY(sample_column);
I hope it helps.
Demo

Return table with columns [duplicate]

This question already has answers here:
function returns multiple columns as a single column instead of multiple columns
(1 answer)
How to return two columns with function
(1 answer)
Record returned from function has columns concatenated
(2 answers)
Return multiple columns and rows from a function PostgreSQL instead of record
(3 answers)
Postgresql function returns composite - how do I access composite values as separate columns?
(2 answers)
Closed 4 years ago.
Here is a simple query that gives expected out.
select 1.1 as x, 1.1 as y;
x | y
-----+-----
1.1 | 1.1
(1 row)
Here the same query is placed inside a function.
CREATE OR REPLACE FUNCTION foo4(param integer)
RETURNS TABLE(x float, y float) AS
$$
DECLARE var float;
BEGIN
var = 1.1;
RETURN QUERY select var as x, var as y;
END;
$$
LANGUAGE 'plpgsql';
Here is the output:
select foo4(4);
foo4
-----------
(1.1,1.1)
(1 row)
Why does the function output the data differently? How can I make the function output into two columns?
Using PostgreSQL 10.3 and Windows 7/10.
select * from foo4(4); should give you the result you are looking for.
Try RETURN QUERY EXECUTE. That will cause the query to be dynamic instead of static. https://www.postgresql.org/docs/current/static/plpgsql-control-structures.html#AEN63012