Git Product home page Git Product logo

Comments (6)

chen-tam avatar chen-tam commented on April 28, 2024 18

func (role Role) Value() (driver.Value, error) {
return string(role), nil
}
func (role *Role) Scan(value interface{}) error {
*role = Role(string(value))
}

from gorm.

zapkub avatar zapkub commented on April 28, 2024 1

@qmn1711 you should provide a correct data type for gorm

type User struct {
	Id    int64
	Email string
	Role  Role `gorm:"type:VARCHAR"`
}

assume as your Role valuer return string

from gorm.

jinzhu avatar jinzhu commented on April 28, 2024

You need to implement a Scanner interface for the new type, then database driver could know how to store the data to database. http://golang.org/pkg/database/sql/#Scanner

from gorm.

leebrooks0 avatar leebrooks0 commented on April 28, 2024

Thanks

On Sun, Jan 5, 2014 at 3:57 PM, Jinzhu [email protected] wrote:

Closed #47 https://github.com/jinzhu/gorm/issues/47.


Reply to this email directly or view it on GitHubhttps://github.com/jinzhu/gorm/issues/47
.

from gorm.

wisecsj avatar wisecsj commented on April 28, 2024
import database/sql/driver

func (role Role) Value() (driver.Value, error) {
return string(role), nil
}
func (role *Role) Scan(value interface{}) error {
value,_ = value.(string)
*role = Role(string(value))
}

More Complete

from gorm.

qmn1711 avatar qmn1711 commented on April 28, 2024

It only works in golang, but in postgresql the role column is still defined as a text when I run db.CreateTable(&User{})

Then I tried to define the custom type for role column

type User struct {
	Id    int64
	Email string
	Role  Role `gorm:"type:role"`
}

This time an error will be thrown: pq: type "role" does not exist

Did I implement the wrong way or gorm haven't covered mapping custom type between golang and postgresql?

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.