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.
Related
This question already has answers here:
Pass column name as parameter to PostgreSQL using psycopg2
(2 answers)
Closed 6 months ago.
I'm using psycopg2, not sqlalquemy. Basically, I can pass parameters to other portions of query, except for the column name. I'm trying to query some columns in order for a particular row, where the column names are enumerated in a list. The problem is the postgres query doesn't work for column names as strings. Any suggestions as to how to approach this problem?
cols = ['first', 'second', 'third']
query = """
SELECT %s FROM table_x
WHERE year=2021;
"""
for c in cols:
cur.execute(query, [c])
print(cur.fetchone()[0])
Using sql module from psycopg2. An example that I believe is more on point and cleaner then the answer posted in the comment.
import psycopg2
from psycopg2 import sql
con = psycopg2.connect(dbname="test", host='localhost', user='postgres', port=5432)
cols = ['first', 'second', 'third']
qry = sql.SQL("SELECT {} FROM table_x WHERE year=2021").format(sql.SQL(", ").join(map(sql.Identifier, cols)))
print(qry.as_string(con))
SELECT "first", "second", "third" FROM table_x WHERE year=2021
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';
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.
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
This question already has answers here:
How to delete an enum type value in postgres?
(10 answers)
Closed 4 years ago.
I found how to add value to the TYPE. But how can I remove value from it?
For example I have TYPE with enum values ('A','B','C'). How to remove 'C'?
To remove value ('val1') from enum ('enum_test') you can use:
DELETE FROM pg_enum
WHERE enumlabel = 'val1'
AND enumtypid = (
SELECT oid FROM pg_type WHERE typname = 'enum_test'
)