Search code examples
postgresqlelixirecto

Good or bad database design


I have following migration:

create table(:countries_codes) do

  add :iso, :string, size: 2
  add :name, :string

  timestamps
end
create unique_index(:countries_codes, [:iso])

create table(:languages_codes) do

  add :iso, :string, size: 2
  add :name, :string

  timestamps

end
create unique_index(:languages_codes, [:iso])

create table(:countries) do

  add :country_iso_id,  references(:countries_codes)
  add :language_iso_id, references(:languages_codes)
  add :name, :string

  timestamps
end
create unique_index(:countries, [:country_iso_id, :language_iso_id])

Would be it better to make iso field on countries_codes and languages_codes as primary key, disable auto generated id field?

create table(:languages_codes, primary_key: false) do

  add :iso, :string, size: 2, primary_key: true
  add :name, :string

  timestamps

end

create table(:countries, primary_key: false) do

  add :country_iso,  references(:countries_codes, column: :iso, type: :string), primary_key: true
  add :language_iso, references(:languages_codes, column: :iso, type: :string), primary_key: true
  add :name, :string

  timestamps
end

create table(:countries_codes, primary_key: false) do

  add :iso, :string, size: 2, primary_key: true
  add :name, :string

  timestamps
end

Solution

  • "better" is subjective, but there is no point in an auto-generated ID when there is already a naturally unique and reasonably short value. Make the codes the primary key and avoid the auto-generated IDs.