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
[-] bjoern_tantau@swg-empire.de 6 points 1 day 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.

[-] ulterno@programming.dev 2 points 22 hours ago

GPS: Galactic Positioning System

Just suppose you are not selling the software outside of the galaxy and you will have to update your database (and transform and migrate all the data) when the 2 galaxy positioning system is formed.

[-] bjoern_tantau@swg-empire.de 2 points 21 hours ago* (last edited 21 hours ago)

Ugh, this will be chaos when we crash into Andromeda. I'm not looking forward to the next billions of years.

[-] ulterno@programming.dev 1 points 18 hours ago

Just complete your KRAs beforehand and hand over the project to the Junior Dev, before then.

[-] deadcream@sopuli.xyz 2 points 1 day 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 4 points 1 day ago

Fine, we'll add altitude as well.

[-] ulterno@programming.dev 1 points 22 hours ago

altitude

Which ones?

[-] TehPers@beehaw.org 5 points 1 day 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 1 day 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 1 day ago

What about alternative universes? 🤣

[-] owenfromcanada@lemmy.ca 1 points 1 day ago

They have their own databases, no additional info required.

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

Programming

20885 readers
57 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