Primary Key for Users per Module? PostgreSQL - 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.

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

“Error 42P01: relation does not exist” (non public schema in 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?

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.

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 );

Doctrine 2 and postgresql, database schema is not in sync

I'm using postgresql in a Symfony2 proyect with Postgis configured in Mac (not sure if that last one makes any difference).
The problem is that validating my schema AFTER running
doctrine:schema:update --force
will result on this error:
[Mapping] OK - The mapping files are correct.
[Database] FAIL - The database schema is not in sync with the current mapping file.
When updating again, Doctrine will try to add everything again, failing of course.
Thanks for the help.
EDIT:
result for app/console --ansi doctrine:schema:update --dump-sql
ALTER T
ABLE actions ADD id VARCHAR(255) NOT NULL;
ALTER TABLE actions ADD display VARCHAR(255) NOT NULL;
ALTER TABLE actions ADD description VARCHAR(255) DEFAULT NULL;
ALTER TABLE actions ADD PRIMARY KEY (id);
-- lots more tables..
ALTER TABLE subclassifications ADD PRIMARY KEY (id);
ALTER TABLE users ADD id INT NOT NULL;
ALTER TABLE users ADD company_id INT DEFAULT NULL;
ALTER TABLE users ADD profile_id INT DEFAULT NULL;
ALTER TABLE users ADD name VARCHAR(255) NOT NULL;
ALTER TABLE users ADD password VARCHAR(255) NOT NULL;
ALTER TABLE users ADD salt VARCHAR(255) NOT NULL;
ALTER TABLE users ADD email VARCHAR(255) NOT NULL;
ALTER TABLE users ADD state INT NOT NULL;
ALTER TABLE users ADD accessCode INT NOT NULL;
ALTER TABLE users ADD CONSTRAINT FK_1483A5E9979B1AD6 FOREIGN KEY (company_id) REFERENCES companies (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE;
ALTER TABLE users ADD CONSTRAINT FK_1483A5E9CCFA12B8 FOREIGN KEY (profile_id) REFERENCES profiles (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE;
CREATE INDEX IDX_1483A5E9979B1AD6 ON users (company_id);
CREATE INDEX IDX_1483A5E9CCFA12B8 ON users (profile_id);
ALTER TABLE users ADD PRIMARY KEY (id);
The point here is that the same dump will work the first time, but when making any change, the dump should be only the incremental changes, not the complete database.
There are a few things that might be wrong here.
First one: which version of Doctrine are you using? Up until 2.3 I had the same thing. Updating to 2.4 fixed it.
Second one: Do you have columnDefinition anywhere in your mapping? If you have it, that's the problem. columnDefinition breaks portability. It also breaks Doctrine Migrations.