Giter VIP home page Giter VIP logo

Comments (7)

kenshaw avatar kenshaw commented on June 9, 2024

UUID is a custom PostgreSQL type. You need to supply a package that supports that type via either the --custom-type-package or -C. That type needs to be Marshal'able by the PostgreSQL Go driver to the database. That's fairly easy to do -- you only need to supply compatible Scanner and Valuer interfaces for that UUID type. You can see an example of that in the generated xo_db.xo.go file, which provides a StringSlice type for working with string arrays in PostgreSQL.

This is an artifact of working with static typing in both PostgreSQL and Go. If you don't want to create a type that supports the Scanner or Valuer interface, then you might want to consider storing your UUIDs as strings (text/varchar/etc) in the database instead, and creating a UUID index over that field, as well as a CHECK() constraint.

from xo.

kenshaw avatar kenshaw commented on June 9, 2024

Oh -- one quick note -- if you want to just quickly generate your schema and later come back to write the Go types for your custom database types, then you can use -C pgtypes, which contains all the PostgreSQL custom defined types and is in the xo/examples/pgtypes directory.

from xo.

whitebook avatar whitebook commented on June 9, 2024

Got it! Thx.

from xo.

odewahn avatar odewahn commented on June 9, 2024

Hi! I'm having the same issue, @whitebook. Do you have an example of how you got this to work?

ps -- thanks for this great package, @kenshaw

from xo.

kenshaw avatar kenshaw commented on June 9, 2024

PostgreSQL's UUID isn't supported in full by the lib/pq driver -- it just sends the read/writes the UUID as a string. All you need to do is define a UUID type supporting the Scanner and Valuer interfaces. You can put this in the same output directory as xo writes to, or you can use some other package that has predefined types and the name of that package via the --custom-type-package command line option.

I'm fairly certain all you need to do is define a type alias in Go like this:

// go/src/github.com/knq/mycustomtypes/mycustomtypes.go
package mycustomtypes

type UUID string

You would just need to then do this:

xo --custom-type-package mycustomtypes -o models pgsql://user:pass@host/dbname

from xo.

odewahn avatar odewahn commented on June 9, 2024

Thanks, @kenshaw. I'll give that a try.

from xo.

odewahn avatar odewahn commented on June 9, 2024

Thanks again for your help, @kenshaw. Just to close the loop in case others have this question, this is what I did:

First, I created a file called "uuid.go" in the same directory as the generated xo models. It's content is:

package models

import (
    "database/sql/driver"
    "errors"
)

// UUID defines the postgres UUID custom type
type UUID struct {
    Val string
}

// Scan satisfies the sql.Scanner interface for UUID.
func (ss *UUID) Scan(src interface{}) error {

    buf, ok := src.([]byte)
    if !ok {
        return errors.New("invalid UUID")
    }

    *ss = UUID{string(buf)}
    return nil

}

// Value satisfies the driver.Valuer interface for UUID.
func (ss UUID) Value() (driver.Value, error) {
    return ss.Val, nil
}

I could then call it using this:

    project, err := models.ProjectByID(db, models.UUID{"d0bf9d77-cd81-4ad6-ace0-f0be928574a2"})

Might not be exactly right, but it seems to be working for me so far.

from xo.

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.