Alter a unique index to set it deferrable in postgresq - postgresql

I need to alter a unique index which has already been created to set it deferrable. In postgres 9.6. Basically, i do something like:
DROP TABLE IF EXISTS test;
CREATE TABLE test (id integer);
ALTER TABLE test ADD CONSTRAINT unique_id unique(id);
ALTER TABLE test ALTER CONSTRAINT unique_id DEFERRABLE INITIALLY DEFERRED;
But i get
ERROR: constraint "unique_id" of relation "test" is not a foreign key constraint
Documentation does not seem to mention that such action cannot be performed. What am i missing?

Per the documentation:
ALTER CONSTRAINT
This form alters the attributes of a constraint that was previously created. Currently only foreign key constraints may be altered.
Instead you can:
ALTER TABLE test DROP CONSTRAINT unique_id;
ALTER TABLE test ADD CONSTRAINT unique_id unique(id) DEFERRABLE INITIALLY DEFERRED;

Related

Getting an error while assigning FK's to tables in postGIS?

I have created 5 tables, with four of them having a Primary Key. I then try to assign Foreign Keys using the following alter statement:
ALTER TABLE SensorLocation
ADD CONSTRAINT constraint_name
FOREIGN KEY (sensor_id)
REFERENCES Sensor (sensor_id)
;
ALTER TABLE DataStream
ADD CONSTRAINT constraint_name
FOREIGN KEY (sensorlocation_id)
REFERENCES SensorLocation (sensorlocation_id)
;
ALTER TABLE DataStream
ADD CONSTRAINT constraint_name
FOREIGN KEY (property_id)
REFERENCES ObservedProperty (property_id)
;
ALTER TABLE Observation
ADD CONSTRAINT constraint_name
FOREIGN KEY (observation_id)
REFERENCES DataStream (observation_id)
;
The thing is, when I only use one constraint per table, it works. But when I want to assign multiple to the table DataStream it doesn't work.
The error message I receive is as follows:
constraint "constraint_name" for relation "datastream" already exists
Does anyone have any idea on how to solve this? All help is welcome!
This is our data-structure:

How to alter a foreign key in postgresql

I created a table in PostgreSQL with a foreign key constraint.
I dropped the table to which the foreign key belongs. Now how to alter the table or how to defer the foreign key present in the table?
To clarify:
I have a table named test. It has a column called subjectName, which is a foreign key of subject Table. Now I dropped subject table. How to remove the FK constaint on table test
Assuming the following tables:
create table subject
(
name varchar(10) primary key
);
create table test
(
some_column integer,
subject_name varchar(10) not null references subject
);
there are two scenarios what could have happened when you dropped the table subject:
1. you didn't actually drop it:
drop table subject;
ERROR: cannot drop table subject because other objects depend on it
Detail: constraint test_subject_name_fkey on table test depends on table subject
Hint: Use DROP ... CASCADE to drop the dependent objects too.
2. you did drop it, then the foreign key is gone as well.
drop table subject cascade;
NOTICE: drop cascades to constraint test_subject_name_fkey on table test
which tells you that the foreign key constraint was automatically dropped.
Perhaps your question in not exactly what you mean. Are you wanting to remove the which was a foreign key from the table. As amply indicated if you dropped the parent table then the FK is also dropped. However the column itself is not dropped from the child table. To remove that you need to alter the table.
alter table test drop column subject_name;
See demo here

Change Index in Postgres

I have been facing a problem on my django application while trying to add an value to a model. It seems that there is a constraint in the DB that should not be there, according to models.py. The error I get is:
IntegrityError: null value in column "column_x_ptr_id" violates not-null constraint
Doing a \dt in psql, I get:
Indexes:
"mytable_model_pkey" PRIMARY KEY, btree (column_x_ptr_id)
"mytable_model_p_key" UNIQUE CONSTRAINT, btree (column_y_ptr_id)
So, my question is how can I modify this index like so?
"mytable_model_pkey" PRIMARY KEY, btree (column_y_ptr_id)
I'm not sure that would solve the problem though..
Ok this will give you a "more or less" of what you need to do. Your table looks something like this:
CREATE TABLE mytable_model
(
column_x_ptr_id integer NOT NULL,
column_y_ptr_id integer,
CONSTRAINT mytable_model_pkey PRIMARY KEY (column_x_ptr_id),
CONSTRAINT mytable_model_p_key UNIQUE (column_y_ptr_id)
)
You need to drop both indexes, create a new PK on the second column, and remove the NOT NULL constraint:
ALTER TABLE mytable_model DROP CONSTRAINT mytable_model_pkey;
ALTER TABLE mytable_model DROP CONSTRAINT mytable_model_p_key;
ALTER TABLE mytable_model ADD CONSTRAINT mytable_model_pkey PRIMARY KEY (column_y_ptr_id);
ALTER TABLE mytable_model ALTER COLUMN column_x_ptr_id DROP NOT NULL;
Bear in mind that adding a primary key to column_y_ptr_id will change the column to NOT NULL. If any records have NULL in that field, this will fail. Then as I mentioned, you will probably want to put another index on column_x_ptr_id for performance reasons. What type you use is up to you.

How to disable foreign key and primary key constraint temporarily in PostgreSQL?

I have one table that has two fields. The structure is like this:
CREATE TABLE raw_links
(
value_id bigint NOT NULL,
raw_id integer NOT NULL,
CONSTRAINT raw_links_pk PRIMARY KEY (raw_id, dp_id),
CONSTRAINT raw_fk FOREIGN KEY (raw_id)
REFERENCES raw_data (raw_data_id) MATCH SIMPLE
ON UPDATE RESTRICT ON DELETE RESTRICT
)
I have to delete 5 million records from this table. For that I want to disable both constraints so that deletion will be faster. After deletion I want to create both constraints.
You can do ALTER TABLE DROP CONSTRAINT raw_links_pk and the same for raw_fk.
After you delete the records, first do a VACUUM ANALYZE raw_links (or VACUUM FULL raw_links if you want to reclaim disk space), to update the table statistics.
Then finally rebuild the constraints with ALTER TABLE ADD CONSTRAINT ....
For the example.
Schema: source, Table: finalsales, Constrait: finalsales_pkey, Column: order_id.
--Disable Primary Key
ALTER TABLE source."finalsales" DROP CONSTRAINT finalsales_pkey;
--Enable Primary Key
ALTER TABLE source."finalsales"
ADD CONSTRAINT finalsales_pkey PRIMARY KEY ( order_id );

How to change a table constraint to deferrable in postgresql?

I have this simple contraint set on my sales table
CONSTRAINT sales_stamping_id_key UNIQUE (company_id, stamping_id, invoice_number, billed)
and i can't seem to find a way to change this constraint to deferrable without deleting the constraint. Is there a way to do this???
You can alter the unique constraint like so:
alter table foo add constraint bar_idx unique using index bar_idx deferrable;
http://www.postgresql.org/docs/current/static/sql-altertable.html