PostgreSQL: Column does not exist after table creation - database

I am trying to import a postgresql database through sql code.
I am creating all the tables with their constraints but when reaching the following code:
COPY "Customers" (Id, "Name") FROM stdin;
psql throws an
ERROR: column "id" of relation "Customers" does not exist.
Here is my Customers table
CREATE TABLE "Customers" (
"Id" serial NOT NULL,
"Name" varchar(30) NOT NULL UNIQUE
);
ALTER TABLE "Customers" OWNER TO postgres;
ALTER TABLE ONLY "Customers"
ADD CONSTRAINT "Customers_pkey" PRIMARY KEY ("Id");
I've just started working with postgre and im totally lost, any help will be much appreciated.

As commented:
COPY "Customers" ("Id", "Name") FROM stdin;
Bad idea to create identifiers wrapped in double quotes. Only do it if they are otherwise illegal.

Related

PostgreSQL Error: Relation already exists - FOREIGN KEY in CREATE TABLE

I am making a table as follows:
CREATE TABLE creator.lists
(
_id bigserial PRIMARY KEY NOT NULL,
account_id bigint NOT NULL,
created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
display_name text DEFAULT '',
name text DEFAULT '',
extra jsonb,
FOREIGN KEY (account_id)
REFERENCES creator.accounts (_id)
ON DELETE CASCADE
);
but I get this error:
ERROR: relation "account_id_index" already exists
When I run:
CREATE INDEX
account_id_index
ON
creator.lists
(
account_id
);
How do I create an index on the foreign key? I am running v11.1
Just a note, that I've also ran a similar command before for another table:
CREATE INDEX
account_id_index
ON
creator.contacts
(
account_id
);
I don't suppose that index names need to be unique between tables?
Indexes live in the same namespace as tables, views and sequences, so you cannot use the same name twice for any of these objects in one schema.
Either choose a different name, or have PostgreSQL choose one for you:
CREATE INDEX ON creator.lists (account_id);
Okay, it seems like index names need to be unique as removing the naming fixed it:
CREATE INDEX
ON
creator.contacts
(
account_id
);
From the documentation:
name
The name of the index to be created. No schema name can be included here; the index is always created in the same schema as its parent table. If the name is omitted, PostgreSQL chooses a suitable name based on the parent table's name and the indexed column name(s).

Foreign key reference problem, SQL Server

I'm having trouble understanding how to setup my foreign key reference. I'm trying to make a reference between tables Lägenheter and Arbetslista so that one row in Lägenheter belong to many rows in Arbetslista.
I have created tables likes this:
CREATE TABLE Deltagare (
Deltagarnr int PRIMARY KEY,
Namn varchar(30),
Typ varchar(30)
);
CREATE TABLE Uppgifter (
Uppgift varchar(30) PRIMARY KEY,
Typ varchar(30),
Pris money
);
CREATE TABLE Arbetslista (
Deltagarnr int not null,
Datum date not null,
Uppgift varchar(30) not null,
Lägenhetsnr int not null,
PRIMARY KEY (Deltagarnr, Datum, Uppgift),
FOREIGN KEY (Deltagarnr) REFERENCES Deltagare(Deltagarnr),
FOREIGN KEY (Uppgift) REFERENCES Uppgifter(Uppgift)
);
Then I import some data from a .mdb file to all three tables.
After that I add another table:
CREATE TABLE Lägenheter(
lägenhetsnummer int PRIMARY KEY,
gatuadress varchar(30),
kvadrater int
);
It all works fine until this last step:
ALTER TABLE Arbetslista
ADD FOREIGN KEY (Lägenhetsnr) REFERENCES Lägenheter(lägenhetsnummer)
;
Here I get this error message:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__Arbetslis__Lägen__403A8C7D". The conflict occurred in database "Hushåll", table "dbo.Lägenheter", column 'lägenhetsnummer'.
After that I have also tried to delete all data from Arbetslista and then execute the mentioned ALTER TABLE again. It works, but then I get an error trying to import the data again:
Hresult: 0x80004005 Description: "The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Arbetslis__Lägen__412EB0B6". The conflict occurred in database "Hushåll", table "dbo.Lägenheter", column 'lägenhetsnummer'.".
(SQL Server Import and Export Wizard)
Why is this not possible when the other foreign keys seems to work fine?
There are rows in Arbetslista table, for which the value of Lägenhetsnr doesn't exists in field lägenhetsnummer of table Lägenheter. You must clean the existing data to be able to create the foreign key like that.
Check your data by executing this script:
select * from Arbetslista a where not exists (select * from Lägenheter l where l.lägenhetsnummer = a.Lägenhetsnr)
After importing the data into Arbetslista, the column Lägenhetsnr was initialize to some value ( say XYZ).
later, when trying to add the FOREIGN KEY to Arbetslista, SQL try to meet this, however it cannot find row with primary key lägenhetsnummer = XYZ and the statement would be terminated.
I think the best is to create the 4 table first then clean the data and try to import it.

SymmetricDS replication referencing wrong schema for foreign key

I use symmetricds replication for postgres database (one server node, one client node). I have got two schemas in my db: 'public' and 'schemaB'. Table from schemaB (tabB) has foreign key referencing field from table in public schema (tabA).
Table definition:
CREATE TABLE schemaB.tabB (
id character varying(255) NOT NULL,
external_id character varying(255),
)
ALTER TABLE ONLY schemaB.tabB
ADD CONSTRAINT fk_ma9cnn7779v065434iugg1321 FOREIGN KEY (external_id) REFERENCES public.tabA(id);
Symmetricds configuration for mentioned table:
insert into sym_trigger (trigger_id, source_schema_name, source_table_name, channel_id, sync_on_incoming_batch, last_update_time, create_time) values
('tid', 'schemaB', 'tabB', 'channel_B', 0, current_timestamp, current_timestamp);
During initial load for client node symmetricds tries to create table with definition:
ALTER TABLE "schemaB"."tabB"
ADD CONSTRAINT "fk_ma9cnn7779v065434iugg1321" FOREIGN KEY ("external_id") REFERENCES schemaB."tabA" ("id");
what causes error:
ERROR: relation "schemaB.users" does not exist. Failed to execute: ALTER TABLE "schemaB"."tabB"
ADD CONSTRAINT "fk_ma9cnn7779v065434iugg1321" FOREIGN KEY ("external_id") REFERENCES schemaB."tabA" ("id")
Symmetricds clearly references wrong schema. Can I do anything about it?
I would suggest having two symmetricDs engines for both schemas public and schemaB that would sync to corresponding schemas on the target node. To create another engine just duplicate the symmetric-ds.properties file, rename it to, let's say symmetric-ds-public.properties and edit to connect to the public schema.

Postgresql problems with postgres_fdw module

I'm trying, using the PostgreSQL Maestro tool, to reference a foreign key coming from a "local" DB to an other primary key inside an other DB (actually, they're both on the same remote machine).
I've heard about the postgres_fdw module to create a foreign table that act like a copy of the table inside the remote DB, but when I try to execute my query I have this error:
"SQL Error: ERROR: referenced relation "foreign_olo" is not a table".
This is my sql code:
CREATE TABLE edb.olo_config (
primary_key integer NOT NULL PRIMARY KEY,
puntamento varchar,
mail_contatto_to varchar,
mail_contatto_cc varchar,
/* Foreign keys */
CONSTRAINT olo_code
FOREIGN KEY (olo_code)
REFERENCES edb.foreign_olo(codice_operatore)
) WITH (
OIDS = FALSE
);
foreign_olo is my foreign table created with postgres_fdw.
I have tried to commit an INSERT or a simple SELECT on the foreign_olo table, and all went well, so I can't understand why for the foreign key case it can't be recognized as a table.
Thank you to everyone would give me an hand!
There are two parts to enforcing a foreign key constraint:
An INSERT (or UPDATE of the FK field) on the child table must check that a parent record exists.
A DELETE (or UPDATE of the PK field) on the parent table must check that no child record exists.
Foreign tables can't be referenced by FK constraints, because this second condition is impossible to enforce - there's no way for the remote server to automatically check for records in your local table.
You can implement the first check relatively easily with a trigger:
CREATE FUNCTION edb.olo_config_fk_trg() RETURNS TRIGGER AS
$$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM edb.foreign_olo
WHERE codice_operatore = new.olo_code
FOR KEY SHARE
)
THEN
RAISE foreign_key_violation
USING MESSAGE = 'edb.foreign_olo.codice_operatore="' || new.olo_code || '" not found';
END IF;
RETURN NULL;
END
$$
LANGUAGE plpgsql;
CREATE TRIGGER olo_config_fk_trg
AFTER INSERT OR UPDATE OF olo_code ON edb.olo_config
FOR EACH ROW EXECUTE PROCEDURE edb.olo_config_fk_trg();
You can create a similar trigger on the PK table to do the update/delete check, but it will require another foreign table in your other database which points back to your local olo_config.

Unable to create diagram

I'm getting this error:
'Category' table saved successfully
'Author' table saved successfully
'Product' table -
Unable to create relationship 'FK_Product_Author'. The ALTER TABLE
statement conflicted with the FOREIGN KEY constraint
"FK_Product_Author". The conflict occurred in database "Sample_1",
table "dbo.Author", column 'AuthorID'.
How can I fix this ??
I suppose you have information in your database and you try to create a diagram.
You have an inconsistence between Product and Author tables, I mean, you have an AuthorId inside Product Table that dooes not exist in Author Table, so when you try to create a Foreign Key, you can't create it.

Resources