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?

top 32 comments
sorted by: hot top controversial new old
[-] tal@lemmy.today 56 points 22 hours ago
[-] einkorn@feddit.org 24 points 22 hours 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 22 hours 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 22 hours 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 22 hours 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 22 hours ago

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

[-] Reddfugee42@lemmy.world 2 points 2 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 3 points 21 hours ago

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

[-] pineapple_pizza@lemmy.dexlit.xyz 31 points 22 hours ago

Store a jpg of the country's flag as binary

[-] Dumhuvud@programming.dev 3 points 2 hours ago

You're supposed to use PNG for images of that sort, you fucking barbarian.

[-] Kissaki@programming.dev 1 points 36 minutes ago

I'll use a gif with each frame being a different country flag. Then I can access them by frame index.

[-] dukatos@lemmy.zip 2 points 5 hours ago

Better use FHD png to keep the quality.

[-] squaresinger@lemmy.world 6 points 7 hours ago

That's what you got emoji for.

[-] owenfromcanada@lemmy.ca 4 points 3 hours ago

Well shit, that's weirdly viable.

[-] squaresinger@lemmy.world 2 points 1 hour ago

It really is.

[-] PolarKraken@programming.dev 2 points 13 hours ago

This should be the standard :)

[-] ExperimentalGuy@programming.dev 10 points 19 hours ago

This is the only real way to do it, the other solutions involve "standards" which more often than not aren't all encompassing. Make sure that any user input of a country is just them uploading the jpg of their home country without any sort of validationbecausee everyone is loyal to their home country.

[-] bleistift2@sopuli.xyz 19 points 22 hours ago

An important question no-one has asked yet is, What do you need that info for?

[-] wise_pancake@lemmy.ca 10 points 22 hours ago

I store iso country codes and use them as the primary key in a countries table.

[-] 6nk06@sh.itjust.works 7 points 22 hours ago

Same for using an "almost non-changing" standard, i.e. either ISO or RFC. And write a script to update the data or tables if something wrong changes (like Russia disappearing because it has been invaded by Belgium), you never know what might happen politically.

[-] rikudou@lemmings.world 4 points 20 hours ago

I'd store it as a string (ISO code), pretty much every programming language has icu data for country names.

If you need more data later, it's very simple to migrate.

[-] owenfromcanada@lemmy.ca -2 points 21 hours ago

GPS coordinates. Much more efficient than storing country, province, city, address, and postal code. Let the front end handle the rest.

[-] squaresinger@lemmy.world 5 points 7 hours ago* (last edited 7 hours ago)

Terrible idea for a few reasons.

  • The example in the OP does not need anything but the country. GPS coordinates are less efficient than ISO codes
  • GPS coordinates don't map 1:1 to countries or even street addresses. There are infinite different coordinates for each address, and it's very non-trivial to match one to another. Comparing whether two records with country codes are in the same country is trivial. Doing the same with two GPS coordinates is very difficult.
  • GPS coordinates might be more exact than accurate. This is a surprisingly common issue: you start out only needing a country, so you put some arvitrary GPS position (e.g. the center of the country) into the GPS coordinates. Later a new requirement arises that means you now need street addresses. Now all old entries point so some random house in the middle of the country, and there's no easy way to differentiate these false locations from real ones.

I guess you meant that as a joke, but people are really doing this and it leads to actual problems.

I saw a news report a while ago about something like that being done in a database for people with outstanding debt. If the address of the debtor wasn't known, they just put "US" in the form, and the program automatically entered the centre of the US as the coordinates.

Sucks for the family that lives there because they constantly get threatening mail and even house visits from angry lenders who want their money back. People even vandalized their house and car because they believed that their debtors lived in that house.

[-] owenfromcanada@lemmy.ca 3 points 5 hours ago
[-] squaresinger@lemmy.world 3 points 3 hours ago

In that case, woosh me. Just wanted to make sure nobody takes that as an actual advice.

[-] bjoern_tantau@swg-empire.de 6 points 19 hours ago

GPS? Absolutely insufficient. What about the people on the ISS? Or when the moon base is established? Ever thought of that? No. You think only of yourself.

[-] deadcream@sopuli.xyz 2 points 5 hours ago

Simple, add additional columns for the frame of reference (e.g. Earth) and elevation. You could even store space coordinates using Sun as a reference point (though you would need to update data regularly for spacecraft as they move of course).

[-] owenfromcanada@lemmy.ca 3 points 18 hours ago

Fine, we'll add altitude as well.

[-] TehPers@beehaw.org 5 points 10 hours ago

You should also include the standardized name of the body the coordinates are relative to. Need to be able to differentiate between lat/long on Jupiter vs on Earth (where lat/long are much more "crunched" aka more precise with shorter floats).

This will be important if intelligent extraterrestrial life is found, or when Musk ships himself to Mars for the good of humanity.

[-] owenfromcanada@lemmy.ca 1 points 5 hours ago

Out of scope for the project, we'll flush it from the backlog and include it in another epic.

[-] vext01@lemmy.sdf.org 2 points 8 hours ago

What about alternative universes? 🤣

[-] owenfromcanada@lemmy.ca 1 points 5 hours ago

They have their own databases, no additional info required.

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

Programming

20858 readers
204 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