Git Product home page Git Product logo

Comments (4)

jinzhu avatar jinzhu commented on April 28, 2024

I will consider in future.

But if you choose char as primary key. then you can't search with db.First(&user, 123) anymore. so don't think it is really helpful for me.

For now, I suggest you just ignore the Id column, and use another field as the primary key.

from gorm.

jinzhu avatar jinzhu commented on April 28, 2024

Add my answer here for a reddit user's question for those users who may interested about this issue.

Could I use uuid (string) as primary key?

No, this is not supported, if so, you will lose the ability to search with primary key. for example:

db.First(&User{}, "blalalalalalaaa")
// db.First(&User{}, 12)
// db.First(&User{}, "age = 20")

We can't know the word "blalalalalalaaa" is sql or primary key. if you want uuid, suggest you could do it like this:

type User struct {
  id int64
  uuid string
}

Then you won't lose the ability to search with primary key. also quite convenient to search with uuid by doing this:

// Find record
db.Where("uuid = ?", "xxxx").First(&User{})

// Find the user, and update some columns
// If not found, then create the record with those attributes
db.Where(User{Uuid: "xxxx"}).Assign("age", 99).FirstOrCreate(&User{})

// Find the user and update some columns
// If not found, initialize the struct with those attributes
db.Where(User{Uuid: "xxxx"}).Assign(User{Age: 99, Email: "[email protected]"}).FirstOrInit(&User{})

from gorm.

regorov avatar regorov commented on April 28, 2024

Hi jinzhu!
Thank you for answer.

Actually, the question about string as primary key is mostly about database design approach. To support natural primary keys or do not at atll. I believe using natural primary keys reasonable in some cases. It makes design simplier. As instance:

type Currency struct {
Id string
Name string
Symbol string
}

type CustomerAccount struct {
Id int64
CurrencyId string
Balance float64
...
}

usd := &Currency{Id : "USD", Name: "USA dollar", Symbol: "$"}

ca := &{CurrrencyId : "USD", Balance : 100}

Anyway, the library is your child, your rules :)

from gorm.

jinzhu avatar jinzhu commented on April 28, 2024
type Currency struct {
  Id         int64
  Code    string
  Name   string
  Symbol string
}

type CustomerAccount struct {
  Id                   int64
  CurrencyCode string
  Balance          float64
}

For me, I prefer write it like this. but anyway, I will look it back after finish other features like indexes, validations, customizable sql type, scopes...

from gorm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.