“Error 42P01: relation does not exist” (non public schema in PostgreSQL) - postgresql

I want to create a foreign key but I got Error 42P01: relation solicitantes does not exist.
I have to schemas: public and laboratorio.
My table is called procedencias. I want to create a foreign key to solicitantes table. Both belongs to laboratorio schema. Even the autocomplete of the target table field works and find the table.
I see that:
I can create the foreing key to any tables of the public schema.
I can't create the foreing key to any tables of the laboratorio schema.
Error when creating a foreign key.
I am using Jetbrains DataGrip software.
This is the DDL of the table called "solicitantes":
CREATE TABLE laboratorio.solicitantes
(
id_solicitante serial NOT NULL,
nombre_solicitante character varying(100) NOT NULL,
CONSTRAINT solicitantes_pkey PRIMARY KEY (id_solicitante)
)
WITH (
OIDS=FALSE
);
ALTER TABLE laboratorio.solicitantes
OWNER TO roby;
CREATE UNIQUE INDEX solicitantes_id_solicitante_uindex
ON laboratorio.solicitantes
USING btree
(id_solicitante);
CREATE UNIQUE INDEX solicitantes_nombre_solicitante_uindex
ON laboratorio.solicitantes
USING btree
(nombre_solicitante COLLATE pg_catalog."default");
I think I have to specify the schema name but I don't know where to do it in DataGrip.

I had the same issue with adding foreign keys to a table in a custom schema from the modify table window. Adding schema to the "Target table" field did not work either. Instead I selected "Open in editor" rather than "Execute in database" and added the schema to the table name in the generated statement and it ran fine.

have you tried to add the explicit schema to the table solicitantes?

Related

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

Make a previously existing foreign key column have a unique constraint in postgres

I need to create a migration for an already existing table to make it's foreign key have a UNIQUE constraint. How do I do this?
From the examples I found in the documentation, it is mostly done when the table is created. The issue is I need to add this onto a column that already exists and is already set as a foreign key. This is what the table looks like at it's creation:
CREATE TABLE IF NOT EXISTS "myTable" (
"_id" SERIAL NOT NULL,
"myForeignKeyId" INTEGER NOT NULL,
"name" VARCHAR(255) NOT NULL,
CONSTRAINT "pk_myTable" PRIMARY KEY ("_id"),
CONSTRAINT "fk_myTable_myForeignKeyId" FOREIGN KEY ("myForeignKeyId") REFERENCES "myOtherTable" ("_id")
);
What I want to do is on a migration make myForeignKeyId unique. How do I do that?
I have tried to following:
CREATE UNIQUE INDEX CONCURRENTLY "myTable_myForeignKeyId"
ON province ("myForeignKeyId");
ALTER TABLE IF EXISTS "myTable"
ADD CONSTRAINT "myForeignKeyId"
UNIQUE USING INDEX "myTable_myForeignKeyId";
First off, when I try this in a migration I get the error:
CREATE INDEX CONCURRENTLY cannot run inside a transaction block
So that part cannot be done, but even just doing it through SQL, the second part doesn't work either as it claims myForeignKeyId already exists. Even if I add an ALTER COLUMN myForeignKeyId it just says there is an error on that line.
This seems like it should be a simple enough operation, how can I do this?
After digging some more found quite a simple way to do this, was clearly originally off target.
To add a unique constraint to a column:
ALTER TABLE "myTable"
ADD CONSTRAINT "myUniqueKeyNameOfChoice" UNIQUE ("myColumn");
To remove it:
ALTER TABLE "myTable"
DROP CONSTRAINT "myUniqueKeyNameOfChoice";

postgres key is not present in table constraint

When trying to ALTER TABLE in Postgres 9.5 to create foreign key constraint: from product_template.product_brand_id to product_brand.id
ALTER TABLE public.product_template
ADD CONSTRAINT product_template_product_brand_id_fkey
FOREIGN KEY (product_brand_id)
REFERENCES public.product_brand (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE SET NULL;
Returns error
ERROR: insert or update on table "product_template" violates foreign key constraint "product_template_product_brand_id_fkey"
DETAIL: Key (product_brand_id)=(12) is not present in table "product_brand".
STATEMENT: ALTER TABLE "product_template" ADD FOREIGN KEY ("product_brand_id") REFERENCES "product_brand" ON DELETE set null
Im confused why postgres is trying to find product_brand.product_brand_id, when the fkey is from product_template.product_brand_id to product_brand.id
Any ideas?
The error message simply states that there is at least one row in the table product_template that contains the value 12 in the column product_brand_id
But there is no corresponding row in the table product_brand where the column id contains the value 12
Key (product_brand_id)=(12) relates the source column of the foreign key, not the target column.
In simple terms, the value of FOREIGN KEY(product_brand_id) provided in your ALTER statement is not present in the source (product_brand) table.

Primary Key for Users per Module? PostgreSQL

I'm making a DB in PostgreSQL and I need a little help.
The user control work with LDAP, and I have a table called modules where I put all the information about the system modules,
Then I created a table called user_module where I put the username and the integer that references a module (in modules table), in this table, you can add/drop rows and I guess I don't need a primary Key for that or isn't it?
I'm using PgAdmin III and it said "I only can View data in this table, I need create a Primary for editing"
Table Code
CREATE TABLE public.adm_mod_usu
(
cusuario text NOT NULL,
cmodulo_det integer NOT NULL,
CONSTRAINT fk_adm_mod_usu_cmodulo_det FOREIGN KEY (cmodulo_det)
REFERENCES public.adm_mod_det (cmodulo_det) MATCH SIMPLE
ON UPDATE RESTRICT ON DELETE RESTRICT,
CONSTRAINT fk_adm_mod_usu_unique_cpermiso UNIQUE (cusuario, cpermiso)
USING INDEX TABLESPACE sistema_index
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.adm_mod_usu
OWNER TO postgres;
GRANT ALL ON TABLE public.adm_mod_usu TO public;
GRANT ALL ON TABLE public.adm_mod_usu TO postgres;
By the help from a_horse_with_no_name:
ALTER TABLE public.adm_mod_usu DROP CONSTRAINT fk_adm_mod_usu_unique_cpermiso;
ALTER TABLE public.adm_mod_usu
ADD CONSTRAINT fk_adm_mod_usu PRIMARY KEY (cusuario, cpermiso)
USING INDEX TABLESPACE sistema_index;
I change the unique constraint to primary.

Postgres foreign keys / schema issue

If I create a new schema on the current database (management), why does it complain about cross-database references?
management=# create schema mgschema;
CREATE SCHEMA
management=# alter table clients add column task_id int null references mgschema.tasks.id;
ERROR: cross-database references are not implemented: "mgschema.tasks.id"
alter table clients add column task_id int null references mgschema.tasks.id;
The REFERENCES syntax in not correct, you should use:
REFERENCES reftable [ ( refcolumn ) ]
A simple references only expects a table name. The foreign key will then automatically point to the primary key of that table, e.g.
alter table clients add column task_id int null references mgschema.tasks;
Another alternative is to to specify the table and columns, but not with a single identifier:
alter table clients add column task_id int null references mgschema.tasks (id);
The second format is needed if the target table has multiple unique constraints.