Home Postgres "there is no unique constraint matching given keys"
Reply: 0

Postgres "there is no unique constraint matching given keys"

Tijdjes123
1#
Tijdjes123 Published in 2017-12-07 14:20:36Z

This question already has an answer here:

  • What is causing ERROR: there is no unique constraint matching given keys for referenced table? 3 answers
  • There is no unique constraint matching given keys for referenced table “employee” 1 1 answer
  • PSQL Error there is no unique constraint matching given keys for referenced table 1 answer
  • ERROR: No unique constraint matching given keys for referenced table 2 answers
  • Postgresql ERROR: there is no unique constraint matching given keys for referenced table 1 answer

I just started using Postgres and right now I have created a file but I get this error

there is no unique constraint matching given keys for referenced table contactgegevens

It needs to look like this: https://imgur.com/a/ub896

But I don't know where the problem is.

Since the tables need to have a column called kaartnummer so I don't know why it shows that error

Right now I have this code:

-- Create schemas

-- Create tables
CREATE TABLE IF NOT EXISTS Persoonsgegevens
(
    Kaartnummer INTEGER UNIQUE,
    Geslacht VARCHAR(5) NOT NULL,
    Voorletter VARCHAR(255) NOT NULL,
    Tussenvoegsel VARCHAR(5) NOT NULL,
    Achternaam VARCHAR(50) NOT NULL,
    Geboortedatum DATE NOT NULL,
    "Voorwaarden Akkoord" BOOLEAN NOT NULL,
    ID INTEGER NOT NULL,
    PRIMARY KEY(ID)
);

CREATE TABLE IF NOT EXISTS Contactgegevens
(
    BSN INTEGER NOT NULL,
    Naam VARCHAR(25) NOT NULL,
    Adres VARCHAR(50) NOT NULL,
    Plaats VARCHAR(40) NOT NULL,
    Straat VARCHAR(40) NOT NULL,
    Telefoonnummer INTEGER NOT NULL,
    Email VARCHAR(50) NOT NULL,
    Kaartnummer INTEGER NOT NULL,
    Huisnummer INTEGER NOT NULL,
    Foto INTEGER NOT NULL,
    PRIMARY KEY(BSN)
);

CREATE TABLE IF NOT EXISTS Reiskosten
(
    Kaartnummer INTEGER NOT NULL,
    Klasse INTEGER NOT NULL,
    Kaartnummer INTEGER NOT NULL,
    PRIMARY KEY(Kaartnummer)
);


-- Create FKs
ALTER TABLE Persoonsgegevens
    ADD    FOREIGN KEY (Kaartnummer)
    REFERENCES Contactgegevens(Kaartnummer)
    MATCH FULL
    ON DELETE CASCADE
    ON UPDATE CASCADE
;

ALTER TABLE Persoonsgegevens
    ADD    FOREIGN KEY (Kaartnummer)
    REFERENCES Reiskosten(Kaartnummer)
    MATCH SIMPLE
    ON DELETE CASCADE
    ON UPDATE CASCADE
;

ALTER TABLE Persoonsgegevens
    ADD    FOREIGN KEY (Achternaam)
    REFERENCES Contactgegevens(Naam)
    MATCH SIMPLE
    ON DELETE CASCADE
    ON UPDATE CASCADE
;


-- Create Indexes
CREATE INDEX Index ON Persoonsgegevens ();
CREATE INDEX Index ON Contactgegevens 
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.309232 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO