Comments (14)
Possibly as a string or bytes if the database doesn't have native support for UUID.
For eg. in MySQL it could be stored as a CHAR(36)
or BINARY(16)
from gorm.
Yes, UUID support is considered, but still not sure have to deal with it for other databases like mysql.
from gorm.
Using the new plugin system, I wonder if there will be an easy way to add custom types? PostgreSQL has a lot of great special types (uuid, xml, json, etc) -- it would be awesome if we could wire up proper handling of them without breaking general support for MySQL and other databases.
from gorm.
@robertmeta Yes, sounds reasonable
from gorm.
+1 :)
from gorm.
@robertmeta @jinzhu any examples for that kind of implementation?
from gorm.
I just created a fork where I add experimental support of UUID ID column for PostgreSQL - https://github.com/8protons/gorm/commit/84b970b6310d51a0798fa4364e4f53ec1026eac3
It affects only PostgreSQL by checking for string type in pg dialect and then - on create by checking id
for slice type (and converting it to string - PostgreSQL returns []uint8 type for uuid).
With this we can use string
type for Id column. In postgresql I use id uuid NOT NULL DEFAULT uuid_generate_v4()
And... it works. Auto-populate Id column on Create and return valid uuid string on Query.
You can test it by:
import "github.com/8protons/gorm"
from gorm.
@reterius Ahh thanks I merged into my fork. Works great.
Don't forget to add PRIMARY KEY
index -> id uuid PRIMARY KEY DEFAULT uuid_generate_v4() NOT NULL
Also added support for Hstore columns using this as reference: https://github.com/lib/pq/blob/master/hstore/hstore.go
and using only ID name as mentioned here in #60
Should find a way to turn this into a plugin soon, but this will do for now.
from gorm.
+1
from gorm.
So if I understand this correctly, we can't currently do something like this?
type Session struct {
Id string `sql:"type:varchar(36);primary key"`
// Otherfields ...
}
And this is because only int64 is supported for primary keys?
from gorm.
varchar is supported as primary key, but you need to initialize the value by yourself. (because varchar doesn't support AUTO_INCREMENT)
So actually postgres's uuid is also supported as primary key, you could config postgres to support it.
from gorm.
Actually this code doesn't work:
package main
import (
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
)
type Session struct {
Id string `sql:"type:varchar(36)"`
UserId int64
}
func main() {
db, _ := gorm.Open("sqlite3", "test.db")
db.LogMode(true)
db.CreateTable(new(Session))
testSession := Session{Id: "test", UserId: int64(1)}
db.Create(&testSession)
}
Running it creates:
(/usr/local/go/src/pkg/runtime/panic.c:248)
[2014-07-10 18:46:57] [0.27ms] INSERT INTO "sessions" ("id","user_id") VALUES ('test','1')
panic: reflect.Set: value of type int64 is not assignable to type string
goroutine 1 [running]:
runtime.panic(0x41ac440, 0xc21005ae00)
/usr/local/go/src/pkg/runtime/panic.c:266 +0xb6
reflect.Value.assignTo(0x41ac1a0, 0x1, 0x60, 0x420fd50, 0xb, ...)
/usr/local/go/src/pkg/reflect/value.go:2198 +0x312
reflect.Value.Set(0x41ac440, 0xc2100567e0, 0x186, 0x41ac1a0, 0x1, ...)
/usr/local/go/src/pkg/reflect/value.go:1385 +0x9b
github.com/jinzhu/gorm.setFieldValue(0x41ac440, 0xc2100567e0, 0x186, 0x41ac1a0, 0x1, ...)
/Users/llovelock/go/src/github.com/jinzhu/gorm/utils.go:115 +0x22e
...
I guess string Id isn't supported?
from gorm.
Hi @levilovelock
Thank you for your feedback, just looked at this issue, seems this is caused by database driver returned int as LastInsertId (but should be string in this case).
Just fixed the problem, please recheck it after upgrade gorm.
from gorm.
@jinzhu - Just tested the fix and it works a charm! Thanks a bunch for that man :)
from gorm.
Related Issues (20)
- How to use interfaces type with Gorm?
- The case when syntax of mssql is not supported HOT 2
- GetIndexes not implemented HOT 5
- btree_gist constraint example
- Wrong table creation order causes migration failure (but not in SQLite)
- More efficient preloading
- Add derived column along with default fields
- 查找不到数据的时候,err与gorm.ErrRecordNotFound比较的结果是false HOT 5
- Update multiple rows with different values in one query in postgres
- Save struct with same relation in associated table HOT 1
- Scan row into multiple structs
- feature: add Chinese README.md
- N+1 Queries with Joins("Company").Preload("Company.Addresses") on Addresses
- What happens when IN clause is too big?
- Document how to use Gorm with Cloud SQL Go Connector HOT 2
- Model() ignores primary key in passed argument
- main.go:5:2: gorm.io/[email protected]: Get "https://proxy.golang.org/gorm.io/gorm/@v/v1.25.9.zip": dial tcp 172.217.163.49:443: i/o timeout HOT 1
- 使用gorm生成的sql能查询到数据,但是返回的结构体里面没有数据 HOT 5
- gorm join时候没有对关联表的软删除进行过滤 HOT 1
- 使用join关联查询数据时候发现关联表没有自动过滤掉软删除的数据
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.