Newbie here having trouble wiring up some tables for a game's leaderboard. Would appreciate help! I have 3 tables; here's an ER diagram:
┌─────┐ ┌─────┐
│ IPs ├─||─────────────────|<─┤Names│
└──┬──┘ └──┬──┘
│ │
│ │
│ │
│ │
│ │
│ ┌──────┐ │
└─||───O<─┤Scores├─|O──────||─┘
└──────┘
Basically, I'd like to have each IP
have multiple name
s, and each score map to an IP
's name
.
However, I run into this error
ERROR: there is no unique constraint matching given keys for referenced table "names"
when I try:
CREATE TABLE IPs(
ip CIDR NOT NULL,
PRIMARY KEY(ip)
);
CREATE TABLE Names(
name VARCHAR(25) NOT NULL,
ip CIDR NOT NULL,
CONSTRAINT ip_fk FOREIGN KEY(ip) REFERENCES IPs(ip),
PRIMARY KEY(name, ip)
);
CREATE TABLE Scores(
score INT NOT NULL,
name VARCHAR(25) NOT NULL,
ip CIDR NOT NULL,
created_at TIMESTAMP NOT NULL,
CONSTRAINT name_fk FOREIGN KEY(name) REFERENCES Names(name),
CONSTRAINT ip_fk FOREIGN KEY(ip) REFERENCES IPs(ip),
PRIMARY KEY(score, name, ip)
);
How should I wire these tables up?