Cross-posted from “What would be the best way to store the country of a user in SQL?” by @[email protected] in [email protected]


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?

  • owenfromcanada@lemmy.ca
    link
    fedilink
    arrow-up
    4
    arrow-down
    5
    ·
    18 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
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      3 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.

    • Björn Tantau@swg-empire.de
      link
      fedilink
      arrow-up
      6
      ·
      16 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
        link
        fedilink
        arrow-up
        2
        ·
        2 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).

        • TehPers@beehaw.org
          link
          fedilink
          English
          arrow-up
          4
          ·
          7 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.