What would be the best way to store the country of a user in SQL?

a year ago by Lena to c/learn_programming

I use Gorm. This is the current code:

package main

import (
    "fmt"
    "log"

    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
)

type Env struct {
    DB     *gorm.DB
    Logger *log.Logger
}

type User struct {
    ID           uint
    Username     string
    Name         string
    Email        string
    PasswordHash string
    Country      string //should probably be a foreign key of another table
}

func initDB() {
    env := &Env{}
    db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})
    if err != nil {
        fmt.Printf("Error opening database: %v", err)
        return
    }
    env.DB = db
    env.DB.AutoMigrate(&User{})

}

func main() {
    initDB()
}

As you can see in the comment in the code, I assume the best way would be to have a table of countries and then assign each user to one via a foreign key. However, it seems a bit cumbersome to manually create a list of all countries. Is there a better way to do this?

Colloidal 9 points a year ago

Which country list will you use? Some countries are not recognized universally. Some countries have different names depending on where you are (Macedonia is known as Former Yugoslavic Republic of Macedonia in Greece and several other countries, mainly because Greece was being bitchy).

The point is, there isn't one best practice because a country list is inherently political. And politics are always messy.

But yeah, a FK to a countries table is reasonable. Good luck.

path: 0 17626852, hotness: undefined, score: 9, children: 1
milkisklim 5 points a year ago

Since 2018 they're actually now The Republic of North Macedonia as per the Prespa Agreement between North Macedonia and the Hellenic Republic.

Which really goes to illustrate your point about politics and show you can't rely on names to be permanent keys for joining anything.

path: 0 17626852 17640237, hotness: undefined, score: 5, children: 0
Penguincoder 5 points a year ago

Give this a read while you're at it.

path: 0 17627013, hotness: undefined, score: 5, children: 1
Colloidal 1 point a year ago

Beautiful.

path: 0 17627013 17642860, hotness: undefined, score: 1, children: 0
6nk06 1 point a year ago
  1. You get a reliable list of official country codes like this one: https://en.wikipedia.org/...
  2. You don't manually import it, you write some kind of script to do that for you
path: 0 17875278, hotness: undefined, score: 1, children: 0
learn_programming
learn_programming

@programming.dev

login for more options
2260
161
46

Posting Etiquette

  1. Ask the main part of your question in the title. This should be concise but informative.

  2. Provide everything up front. Don't make people fish for more details in the comments. Provide background information and examples.

  3. Be present for follow up questions. Don't ask for help and run away. Stick around to answer questions and provide more details.

  4. Ask about the problem you're trying to solve. Don't focus too much on debugging your exact solution, as you may be going down the wrong path. Include as much information as you can about what you ultimately are trying to achieve. See more on this here: https://xyproblem.info/

Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient

go to feed...