I am attempting to create a relational database to model fight sports information--contests between two opponents, each with three judges and a referee. I have tables that store information on each entity: Participant, Judge, and Referee. I also have a table that models the Contest itself.
How do I manage the relationships between these tables where Participants may participate in multiple contests, judges may judge multiple contests and referees may referee multiple contests?
Here is a minimal example of what I am trying to do.
This is the dbml:
Table Contest{
contest_id[pk, increment]
participant_1_id int [ref: > Partcipant.participant_id ]
participant_2_id int [ref: > Partcipant.participant_id ]
}
Table Participant{
participant_id[pk, increment]
participant_wins int
participant_losses int
participant_draws int
}
Here is the generated SQL for PostgreSQL:
CREATE TABLE "Contest" (
"contest_id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"participant_1_id" int,
"participant_2_id" int
);
CREATE TABLE "Participant" (
"participant_id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"participant_wins" int,
"participant_losses" int,
"participant_draws" int
);
ALTER TABLE "Contest" ADD FOREIGN KEY ("participant_1_id") REFERENCES "Participant" ("participant_id");
ALTER TABLE "Contest" ADD FOREIGN KEY ("participant_2_id") REFERENCES "Participant" ("participant_id");
Here are the error messages I get:
CREATE TABLE "Contest" (
"contest_id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"participant_1_id" int,
"participant_2_id" int
);
psql:database/db.sql:5: ERROR: relation "Contest" already exists
CREATE TABLE "Participant" (
"participant_id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"participant_wins" int,
"participant_losses" int,
"participant_draws" int
);
psql:database/db.sql:12: ERROR: relation "Participant" already exists
ALTER TABLE "Contest" ADD FOREIGN KEY ("participant_1_id") REFERENCES "Participant" ("participant_id");
psql:database/db.sql:14: ERROR: column "participant_1_id" referenced in foreign key constraint does not exist
ALTER TABLE "Contest" ADD FOREIGN KEY ("participant_2_id") REFERENCES "Participant" ("participant_id");
psql:database/db.sql:16: ERROR: column "participant_2_id" referenced in foreign key constraint does not exist