Comments (7)
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.
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.
Got it! Thx.
from xo.
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.
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.
Thanks, @kenshaw. I'll give that a try.
from xo.
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)
- How to generate postgres functions under schema other than public
- xo schema scan is failing. HOT 1
- BPMN HOT 1
- Could you add the Comment of each Field of migration table to generated xo files ? HOT 6
- Can I generate only struct? (without CRUD methods)
- [Bug] array type is not generated correctly for integer array in case of postgres HOT 1
- error: createdb: unable to import package: 1:22: import "createdb" error: unable to find source related to: "createdb" HOT 5
- invalid memory address or nil pointer dereference
- Procedure code generation may have issues HOT 1
- _exists field in code generated code
- Not run go 1.19.5 on windows HOT 1
- run xo fails on windows os HOT 7
- Unable to run because of createdb error HOT 2
- [Question] List all Indexes from table inside typedef template HOT 1
- "LIMIT %%limit int%%" in MySQL
- Does xo support camelcase json tags? HOT 3
- Postgres foreign keys not working if name is the same HOT 2
- Postgres insert/update RETURNING HOT 1
- Type mapping HOT 1
- Custom types... again HOT 2
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 xo.