I have these tables. There are many languages, and many countries.
Every country has many (some subset of) languages.
create table countries (
country_id serial primary key,
country_name varchar(128) unique not null
);
create table languages (
language_id serial primary key,
language_name varchar(128) unique not null
);
create table countries_languages (
country_id int references countries,
language_id int references languages,
primary boolean not null default false,
unique(country_id, language_id)
);
Politics clouding reality as it always does, each country must be
assigned a primary language. That's the primary column in
countries_languages. This column doesn't exist yet, but is the best
way I can think of to solve the problem.
For each country there should be no more than one row in
countries_languages for which primary is true.
How do I enforce it in the database? Is there a clean and portable way
to do it? Or at least a clean postgresql-only way to do it?
I don't want to make a separate one-to-(zero or one) table, because
all the important stuff will still work with multiple languages per
country. If there is a separate countries_primarylanguage table, it
must be guaranteed consistent with countries_language.