Let's say I have the following tables:
create table repositories (
id serial primary key,
name varchar(255) not null
);
create table remotes (
id serial primary key,
url varchar(255) unique not null,
repository_id integer references repositories(id) not null
);
I want to have an "origin" remote for a repository.
I have 2 solutions:
Create a circular dependency, which means using
ALTER TABLEin the schema:alter table repositories add origin_remote_id integer references remotes(id);Add a flag on
remoteswith a customCHECKconstraint:create table remotes ( id serial primary key, url varchar(255) unique not null, repository_id integer references repositories(id) not null, is_origin boolean not null constraint is_only_origin check (is_only_origin(repository_id)) );
The problem is that the 1st approach makes relationally more sense, but it also introduces a circular dependency.
Is it fine? I'm not sure what I should do.
repositories -> remotesFK in the 1st solution is wrong, it should include two columns. – ypercubeᵀᴹ May 05 '15 at 15:00is_originset to true. – ypercubeᵀᴹ May 05 '15 at 15:12