16

Cross-posted from "What would be the best way to store the country of a user in SQL?" by @lena@gregtech.eu in !learn_programming@programming.dev


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?

you are viewing a single comment's thread
view the rest of the comments
[-] tal@lemmy.today 57 points 1 day ago
[-] einkorn@feddit.org 26 points 1 day ago

This and nothing else. Had to deal with way too many APIs that would use some sort of homebrew schema.

[-] lena@gregtech.eu 4 points 1 day ago

Am I supposed to make an SQL statement that puts these country codes into a table, along with the country's name? There's probably a better way. Maybe I could make a new entry for every unique country a user is from

[-] deadcream@sopuli.xyz 29 points 1 day ago

Why do you need to store the name of a country in the database? Frontend can take the country code and display a full name on its own, and do it in a localized way too.

[-] schnurrito@discuss.tchncs.de 7 points 1 day ago

not sure I understand the distinction between the "am I supposed to" and "maybe I could" parts?

You should create a table of all countries, you can just copy that from the above link. Then you reference that table with a foreign key in your users table.

[-] lena@gregtech.eu -1 points 1 day ago

Sooooo I copy paste every single country code and put it in a table?

[-] Reddfugee42@lemmy.world 2 points 19 hours ago

If you can't figure out how to get a foreign table into your database let someone who knows databases do it for you

[-] schnurrito@discuss.tchncs.de 4 points 1 day ago

How exactly you create that table is up to you of course, I don't know enough about your project setup.

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

Programming

20885 readers
83 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 2 years ago
MODERATORS