We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
type User struct{ ID int64 `gorm:"column:id;primaryKey"` UserId int64 `gorm:"column:user_id"` } shard, err := sharding.Register(sharding.Config{ ShardingKey: "user_id", NumberOfShards: 1, PrimaryKeyGenerator: sharding.PKSnowflake, ShardingAlgorithm: getShardFn(1), }, "users")) if err != nil { return nil, err } if err := db.Use(shard); err != nil { return nil, err } result := db.Create(&user) // pass pointer of data to Create user.ID // 0
The text was updated successfully, but these errors were encountered:
解决了吗
Sorry, something went wrong.
@hyperphoton 看看这个问题
The problem is that when result := db.Create(&user) is executed there is an error at the database level, but you didn't check it, so the user.ID is 0.
result := db.Create(&user)
Here is a worked example:
package main import ( "fmt" "gorm.io/driver/postgres" "gorm.io/gorm" "gorm.io/sharding" ) type User struct { ID int64 `gorm:"column:id;primaryKey"` UserId int64 `gorm:"column:user_id"` } func main() { dsn := "postgres://localhost:5432/sharding-db?sslmode=disable" db, _ := gorm.Open(postgres.New(postgres.Config{DSN: dsn})) shard := sharding.Register(sharding.Config{ ShardingKey: "user_id", NumberOfShards: 1, PrimaryKeyGenerator: sharding.PKSnowflake, }, "users") db.Use(shard) user := User{UserId: 42} err := db.Create(&user).Error // pass pointer of data to Create if err != nil { panic(err) } fmt.Println(user.ID) // 1546765735429144576 }
jinzhu
No branches or pull requests
Your Question
The document you expected this should be explained
Expected answer
The text was updated successfully, but these errors were encountered: