this post was submitted on 12 Jun 2025
11 points (100.0% liked)

Learn Programming

1874 readers
1 users here now

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

founded 2 years ago
MODERATORS
 

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?

top 4 comments
sorted by: hot top controversial new old
[–] [email protected] 8 points 5 days ago* (last edited 5 days ago) (1 children)

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.

[–] [email protected] 4 points 4 days ago* (last edited 4 days 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.

[–] [email protected] 5 points 5 days ago* (last edited 5 days ago) (1 children)

Give this a read while you're at it.

[–] [email protected] 1 points 4 days ago