Comments (4)
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.
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.
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.
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)
- Many2Many associations cause AutoMigrate to create joining tables with the wrong data types; then foreign key constraints fail. HOT 2
- Lazy Connections with Read/Write splitting
- Initialization with no default dialector
- gorm 1.25.1 版本,当表结构发生变更时,调用AutoMigrate方法时出现报错:got 1 parameters but the statement requires 0 HOT 1
- With gorm and the sqlite driver that comes with gorm, when storing a number of the string type, if the number is very long, then the number is stored in the database as a scientific notation
- 针对下列复杂SQL查询,GORM怎么处理? HOT 3
- mysql JSON_SET cannot be used in many ways HOT 2
- “删除关联”无法正常工作 HOT 1
- 单列索引在不指定索引名时索引名生成规则用的是定义field name,而不是用的 column name HOT 5
- Joins 必须要写sql语句?好呆的设计啊 HOT 5
- I want to implement global error handling in Gorm
- how to control timeout for every single sql statement,like create,update,query,raw sql HOT 1
- Transaction not commit under certain special circumstances. HOT 1
- gorm gen 生成的 entity, 应该怎么写 hooks 呢? HOT 8
- autoIncrementIncrement not work in postgres
- The plugin BeforeCreate conflicts with the model's BeforeCreate, and the program crashes HOT 1
- Callback().Update() 不生效 HOT 1
- 内存泄漏 HOT 1
- how to add comment for a table(not field) by using automigrate?
- model只有一个字段并设置为unique,使用AutoMigrate,第二次启动服务Migrate会报错 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gorm.