Git Product home page Git Product logo

Comments (14)

dre1080 avatar dre1080 commented on April 28, 2024 3

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.

jinzhu avatar jinzhu commented on April 28, 2024

Yes, UUID support is considered, but still not sure have to deal with it for other databases like mysql.

from gorm.

robertmeta avatar robertmeta commented on April 28, 2024

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.

jinzhu avatar jinzhu commented on April 28, 2024

@robertmeta Yes, sounds reasonable

from gorm.

gravis avatar gravis commented on April 28, 2024

+1 :)

from gorm.

dre1080 avatar dre1080 commented on April 28, 2024

@robertmeta @jinzhu any examples for that kind of implementation?

from gorm.

 avatar commented on April 28, 2024

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.

dre1080 avatar dre1080 commented on April 28, 2024

@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.

degenhard avatar degenhard commented on April 28, 2024

+1

from gorm.

levpaul avatar levpaul commented on April 28, 2024

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.

jinzhu avatar jinzhu commented on April 28, 2024

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.

levpaul avatar levpaul commented on April 28, 2024

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.

jinzhu avatar jinzhu commented on April 28, 2024

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.

levpaul avatar levpaul commented on April 28, 2024

@jinzhu - Just tested the fix and it works a charm! Thanks a bunch for that man :)

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.