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?
Terrible idea for a few reasons.
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.
Yep, meant it as a joke.
In that case, woosh me. Just wanted to make sure nobody takes that as an actual advice.