Comments (6)
func (role Role) Value() (driver.Value, error) {
return string(role), nil
}
func (role *Role) Scan(value interface{}) error {
*role = Role(string(value))
}
from gorm.
@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.
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.
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.
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.
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)
- GORM save to map causes crash when model type includes primary key field (solution shown) HOT 3
- Why Pluck column type is time.Time value no results, but column type is string value have results. HOT 2
- 批量创建时,当传递的dst的元素是Interface时报错 HOT 2
- Why does db.RowsAffected return 0 after the data is updated? HOT 1
- Why does db.RowsAffected return 0 after the data is updated? HOT 7
- 多次调用Model方法 只有第一次调用更新了表名 后续调用Model方法无效 HOT 1
- GORM无法操作数据库的视图,无法想映射Molde一样映射一个Struct类型来操作数据库创建的视图结构,并进行CRUD!拜托!这么重要的功能都没有! HOT 1
- Inner join with condition
- Can't get data from columns that names contain numbers HOT 4
- betch save model, on conflict by uniqueIndex, return id err HOT 1
- betch save model, on conflict by uniqueIndex, return id err
- one to many with subquery join does not scan to parent struct HOT 1
- prepareStmt is not support for ping function HOT 3
- ERROR: prepared statement "lrupsc_1_20" already exists, and plansource is not the same. (SQLSTATE 42P05) HOT 1
- 升级gorm 使用postgres后报错 LastInsertId is not supported by this driver HOT 5
- Bug: SetColumn can possible update wrong column I BeforeUpdate hook
- Duplicate records with the same primary key returned in a short period of time HOT 2
- postgres use gorm v1.25.9 生成的update语句有误 HOT 4
- i get a error : ERROR: prepared statement "lrupsc_1_20" already exists, and plansource is not the same. HOT 1
- AutoMigrate to add primary key is not working HOT 3
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.